开发者

JNDI jdbc data source in embedded tomcat 7

I've a web application with database data source defined in 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>

and this is my 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>

and this my how I run Tomcat (7.0.8) in my test code:

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

There is a tomcat.8080 directory in my workspace with webapps and work directory. The ROOT points to src/main/webapp (all Java code is in classpath).

The problem is I get following exception when I run this code:

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

What am I doing wrong?

Thank you.


I think the config file (context.xml) doesn't get resolved automatically by the embedded Tomcat implementation even if it sits in the default location (META-INF). If you set the config file programmatically your application should be able to create the datasource. Make sure the path to your context.xml is correct. Here's an example:

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


BTW, you can use mocked JNDI context from Spring Test framework, will work for embedded tomcat as well, eq :

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


I had the same problem with this configuration:

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

caused by: javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

I finally make it work changing the configuration to this:

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

Yeah, it is ridiculous, I don't know if it's part of the specification, but the name has to have the first letter in upper case. I saw you're using "jdbc/mkddb", try to use "jdbc/Mkddb", and don't forget to change all references to the name.

I hope that was your solution, JNDI errors are quite cumbersome

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜