Hibernate NameNotFound异常-Spring启动(最新)、嵌入式Tomcat、JNDI

Hibernate NameNotFound异常-Spring启动(最新)、嵌入式Tomcat、JNDI,hibernate,spring-boot,jndi,embedded-tomcat-8,Hibernate,Spring Boot,Jndi,Embedded Tomcat 8,我的症状类似于: 我创建了一个github repo,重新创建了我的问题:(注意,您可能需要手动添加oracle依赖项,因为oracle阻止将其JAR发布到公共maven repo) 克隆repo、cding到它和mvn clean&mvn包&java-jar target/spring-boot-sample-tomcat-jndi-0.1.0-SNAPSHOT.jar将复制NameNotFound执行选项 有人能帮我了解为了使用tomcat 8和最新的spring boot版本以及jndi

我的症状类似于:

我创建了一个github repo,重新创建了我的问题:(注意,您可能需要手动添加oracle依赖项,因为oracle阻止将其JAR发布到公共maven repo)

克隆repo、cding到它和mvn clean&mvn包&java-jar target/spring-boot-sample-tomcat-jndi-0.1.0-SNAPSHOT.jar将复制NameNotFound执行选项

有人能帮我了解为了使用tomcat 8和最新的spring boot版本以及jndi(可能是hibernate),需要更改哪些配置吗?

问题的详细说明:

以前的答案中的解决方案对我不起作用,我怀疑新版本中存在依赖性问题,或者与hibernate如何/何时执行jndi查找有关

然而,我已经克隆了它的spring引导依赖项,并将其更新为最新版本,更新了这个新版本产生的tomcat包/导入,从tomcat 7迁移到tomcat 8,并使其运行

我怀疑我的问题略有不同,尽管异常表现是相同的

我收到的例外情况是:

Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
        at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
        at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
        at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
        ... 47 more
Caused by: org.hibernate.engine.jndi.JndiException: Unable to lookup JNDI name [java:comp/env/jdbc/MyDataSource]
        at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:100)
        at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:98)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
        ... 65 more
Caused by: javax.naming.NameNotFoundException: Name [comp/env/jdbc/MyDataSource] is not bound in this Context. Unable to find [comp].
        at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
        at org.apache.naming.SelectorContext.lookup(SelectorContext.java:134)
        at javax.naming.InitialContext.lookup(Unknown Source)
        at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:97)
        ... 74 more
我正在将一个遗留的Struts应用程序迁移到SpringMVC,最好是使用嵌入式tomcat的SpringBoot。最终,我计划将这些jndi资源查找更改为(至少我所知道的)传统定义的数据源,但现在希望避免这一步

我正在导入applicationContext.xml资源:

@Configuration
@ComponentScan
@EnableAutoConfiguration
@ImportResource("classpath:context/applicationContext.xml")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public TomcatEmbeddedServletContainerFactory tomcatFactory() {
        return new TomcatEmbeddedServletContainerFactory() {

            @Override
            protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                    Tomcat tomcat) {
                tomcat.enableNaming();
                return super.getTomcatEmbeddedServletContainer(tomcat);
            }

            @Override
            protected void postProcessContext(Context context) {
                ContextResource resource = new ContextResource();
                ContextResourceLink resourceLink = new ContextResourceLink();
                resource.setName("jdbc/MyDataSource");
                resource.setProperty("global", "jdbc/MyDataSource");
                resource.setType("javax.sql.DataSource");
                resource.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver");
                resource.setProperty("url", "jdbc:yourDb");
                resource.setAuth("Container");

                context.getNamingResources().addResource(resource);

                resourceLink.setName("jdbc/MyDataSource");
                resourceLink.setType("javax.sql.DataSource");
                resourceLink.setProperty("driverClassName",  "oracle.jdbc.driver.OracleDriver");
                resourceLink.setProperty("url", "jdbc:connectionString");

                context.getNamingResources().addResourceLink(resourceLink);

                System.out.println("\n\n\ncontext naming resources\n"+context.getResourceOnlyServlets()+"\n\n\n");
            }
        };
    }

    @Bean(destroyMethod="")
    public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
        System.out.println("\n\n\nIn jndiDataSource\n\n\n");
        JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        bean.setJndiName("java:comp/env/jdbc/MyDataSource");
        bean.setProxyInterface(oracle.jdbc.driver.OracleDriver.class);
        bean.setLookupOnStartup(false);
        bean.afterPropertiesSet();
        return (DataSource)bean.getObject();
    }
}
资源applicationContext.xml引用一个hibernateContext.xml,该文件引用的hibernate.cfg.xml文件如下所示:

<hibernate-configuration>
    <session-factory>
        <property name="connection.datasource">java:comp/env/jdbc/MyDataSource</property>
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
        <property name="show_sql">false</property>
        <property name="format_sql">true</property>
        <property name="hibernate.max_fetch_depth">2</property>
.....
豆子。我确实看到:

Name = MyDataSource Ignoring unknown property: value of "jdbc/MyDataSource" for "global" property
认识到这不是一个有效或有用的设置,但这是我尝试过的事情之一

如果您能帮我找出问题的原因和解决方案,我将不胜感激——我以前没有使用过JNDI资源,而且这个应用程序的背景是WAS

我已经看过了大多数堆栈溢出问题,这些问题与spring boot、hibernate、jndi、name not found的一些组合不太匹配,并且阅读了tomcat资源定义文档,但是还没有将它们组合起来


如果我遗漏了任何问题,请告诉我。我也有同样的问题。我建议首先尝试更改JndiObjectFactory上的jndiName。如前所述,它应该是jdbc/myDataSource,而不是java:comp/env/jdbc/myDataSource。看起来注册会自动将资源放在java:comp/env/下面。这对我很管用

您还可以在应用程序中定义数据源。属性

spring.datasource.jndi-name=jdbc/myDataSource
而不是在应用程序类中执行此操作:

@Bean(destroyMethod="")
public DataSource jndiDataSource() {
...
   bean.setJndiName("jdbc/myDataSource");
...
}
为了解决我的错误,我还必须使用与您复制的示例不同的依赖项。我从tomcat 8.0.23切换到7.0.56,并切换到tomcat dbcp 7.0.47:

母体聚甲醛

<properties>
    <java.version>1.7</java.version>
    <tomcat.version>7.0.56</tomcat.version>
    <boot.version>1.2.5.RELEASE</boot.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>com.My_App</start-class>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <mainClass>${start-class}</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>${boot.version}</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

有了这些依赖项,我得到了一个行context.getNamingResources().addResource(resource)的NoSuchMethodError;(添加资源)。另外,我对后处理setJndiName的更改感到惊讶——引导团队成员wilksona在git项目中使用了完全限定名,wilksona在回答这些堆栈溢出问题时发表了文章。你能发布你使用的完整POM(或者至少是SpringBoot加上tomcat依赖项的版本)吗?在我的评论底部,你可以看到我使用的导入。评论的(…web.ContextResource)对我不起作用。试试看,我想要一个理想的解决方案,它允许我继续使用最新版本的spring boot和tomcat 8.x.x
<properties>
    <java.version>1.7</java.version>
    <tomcat.version>7.0.56</tomcat.version>
    <boot.version>1.2.5.RELEASE</boot.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>com.My_App</start-class>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <mainClass>${start-class}</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>${boot.version}</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>
<parent>
    <groupId>com</groupId>
    <artifactId>WebModule</artifactId>
    <version>1.2-SNAPSHOT</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
            <exclusion>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <groupId>org.springframework.boot</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator</artifactId>
    </dependency>       
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>7.0.56</version><!--$NO-MVN-MAN-VER$--> 
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-el</artifactId>
        <version>7.0.56</version><!--$NO-MVN-MAN-VER$--> 
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-logging-juli</artifactId>
        <version>7.0.56</version><!--$NO-MVN-MAN-VER$--> 
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-websocket</artifactId>
        <version>7.0.56</version><!--$NO-MVN-MAN-VER$--> 
    </dependency>
    <dependency> 
        <groupId>org.apache.tomcat</groupId> 
        <artifactId>tomcat-dbcp</artifactId> 
        <version>7.0.47</version> 
        <scope>provided</scope>
    </dependency>     
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>
// import org.apache.tomcat.util.descriptor.web.ContextResource;
import org.apache.catalina.deploy.ContextResource;