嵌入式tomcat 7中的JNDI jdbc数据源

时间:2011-03-08 21:18:19

标签: tomcat jdbc datasource jndi

我有一个带有数据库数据源的Web应用程序,在META-INF / context.xml中定义:

<Context>
    <Resource
        name="jdbc/mkddb"
        auth="Container"
        type="javax.sql.DataSource"
        maxActive="10"
        maxIdle="5"
        maxWait="10000"
        driverClassName="org.postgresql.Driver"
        username="woky"
        password="XYZ"
        url="jdbc:postgresql://[::1]:5433/mkddb" />
</Context>

这是我的peristence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="myJpaUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:comp/env/jdbc/mkddb</non-jta-data-source>
        <class>mkd.model.User</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        </properties>
    </persistence-unit>
</persistence>

这就是我在测试代码中运行Tomcat(7.0.8)的方法:

Tomcat tomcat = new Tomcat();
tomcat.addWebapp("", "ROOT");
tomcat.enableNaming();
tomcat.start();
System.in.read();
tomcat.stop();

我的工作区中有一个tomcat.8080目录,其中包含webapps和工作目录。 ROOT指向src / main / webapp(所有Java代码都在类路径中)。

问题是当我运行此代码时出现以下异常:

javax.persistence.PersistenceException: [PersistenceUnit: myJpaUnit] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:892)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    at com.google.inject.persist.jpa.JpaPersistService.start(JpaPersistService.java:94)
    at com.google.inject.persist.PersistFilter.init(PersistFilter.java:77)
    at com.google.inject.servlet.FilterDefinition.init(FilterDefinition.java:114)
    at com.google.inject.servlet.ManagedFilterPipeline.initPipeline(ManagedFilterPipeline.java:98)
    at com.google.inject.servlet.GuiceFilter.init(GuiceFilter.java:172)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:273)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:254)
    at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:372)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:98)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4461)
    at org.apache.catalina.core.StandardContext$2.call(StandardContext.java:5133)
    at org.apache.catalina.core.StandardContext$2.call(StandardContext.java:5128)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.hibernate.HibernateException: Could not find datasource
    at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:79)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:137)
    at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:51)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2163)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2159)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1383)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:883)
    ... 20 more
Caused by: javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
    at org.apache.naming.NamingContext.lookup(NamingContext.java:803)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:145)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:814)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:145)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:814)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
    ... 28 more

我做错了什么?

谢谢。

3 个答案:

答案 0 :(得分:7)

我认为配置文件(context.xml)不会被嵌入式Tomcat实现自动解析,即使它位于默认位置(META-INF)。如果以编程方式设置配置文件,则应用程序应该能够创建数据源。确保context.xml的路径正确无误。这是一个例子:

File configFile = new File("META-INF/context.xml")
Context context = tomcat.addWebapp("", "ROOT");
context.setConfigFile(configFile.toURI().toURL());

答案 1 :(得分:3)

BTW,您可以使用Spring Test框架中的模拟JNDI上下文,也适用于嵌入式tomcat,例如:

OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:@127.0.0.1:1521/orcldb");
ods.setUser("*");
ods.setPassword("*");
SimpleNamingContextBuilder builder = null;
    try {
        builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
        builder.bind("java:comp/env/jdbc/db",ods);
    } catch (NamingException e) {
        logger.error(e);
    }

答案 2 :(得分:0)

我遇到了与此配置相同的问题:

<Resource name="jdbc/casper" ... ... />

由以下原因引起:javax.naming.NameNotFoundException:名称jdbc未绑定在此Context中

我最终将配置更改为:

<Resource name="jdbc/Casper" ... ... />

是的,这很荒谬,我不知道它是否是规范的一部分,但名称必须以大写字母表示第一个字母。我看到你正在使用“jdbc / mkddb”,尝试使用“jdbc / Mkddb”,并且不要忘记更改对该名称的所有引用。

我希望那是你的解决方案,JNDI错误非常繁琐