NameNotFound Exception - Spring Boot(LATEST),嵌入式Tomcat,JNDI

时间:2015-10-09 20:23:32

标签: hibernate spring-boot jndi embedded-tomcat-8

我的症状类似于: How to create JNDI context in Spring Boot with Embedded Tomcat Container

我创建了一个github repo来重新创建我的问题:https://github.com/Rkiouak/spring-boot-embedded-tomcat-hibernate-jndi (注意,您可能需要手动添加oracle依赖项,因为oracle阻止将其jar发布到公共maven回购)

克隆回购物,放入其中并使用mvn clean&& amp; mvn package&& java -jar target / spring-boot-sample-tomcat-jndi-0.1.0-SNAPSHOT.jar将重现NameNotFound execption。

任何人都可以帮助我理解为了使用tomcat 8需要更改哪些配置以及最新的带有jndi的spring-boot版本(可能是hibernate复杂的东西)?

问题的更长描述:

之前答案的解决方案对我没有用,我怀疑新版本中存在依赖问题,或者与hibernate执行jndi查找的方式有关。

但是,我克隆了https://github.com/wilkinsona/spring-boot-sample-tomcat-jndi,将其春季启动依赖关系更新为LATEST,更新了此新版本产生的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应用程序迁移到Spring MVC,最好是使用嵌入式tomcat进行Spring启动。最终我打算将这些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>
.....

我没有看到

中的println
@Bean(destroyMethod="")
public DataSource jndiDataSource()...

豆。我确实看到了:

Name = MyDataSource Ignoring unknown property: value of "jdbc/MyDataSource" for "global" property

并认识到这不是一个有效或有用的设置,但这是我尝试过的事情之一。

任何有关识别1)原因和2)解决方案的帮助都将非常感激 - 我之前没有使用过JNDI资源,这个应用程序的背景是WAS。

我已经查看了大多数堆栈溢出问题,这些问题模糊地匹配了spring-boot,hibernate,jndi,未找到的名称以及通过tomcat资源定义文档读取的一些组合,但是还没有找到能够把它拼凑起来......

如果我遗漏了任何

,请告诉我

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我建议首先尝试更改JndiObjectFactory上的jndiName。正如here所解释的那样,它应该是jdbc / myDataSource,而不是java:comp / env / jdbc / myDataSource。看起来注册会自动将资源放在java:comp / env /下。这对我有用。

您还可以在 application.properties

中定义数据源
spring.datasource.jndi-name=jdbc/myDataSource

而不是在Application类中执行:

@Bean(destroyMethod="")
public DataSource jndiDataSource() {
...
   bean.setJndiName("jdbc/myDataSource");
...
}

要解决我的错误,我还必须使用与您复制的示例不同的依赖项。我从tomcat 8.0.23切换到7.0.56并切换到tomcat-dbcp 7.0.47:

父母pom

<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;