简体   繁体   中英

Hibernate 4.3 + Tomcat 7 Unable to lookup JNDI name

I have a trouble upgrading to Hibrernate 4.3.x from 4.2.7. I got this exception:

Caused by: javax.naming.NameNotFoundException: Name [java:comp/env/jdbc/data] is not bound in this Context. Unable to find [java:comp].
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
at javax.naming.InitialContext.lookup(InitialContext.java:415)
at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:114)
... 82 more

I'm using Tomcat 7.0.29 (tried 7.0.47 as well) and JDK 7 (v25). There is no problem with Hibernate 4.2.7.

Here is my persistence.xml:

<persistence-unit name="data" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>java:comp/env/jdbc/data</non-jta-data-source>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
    </properties>
</persistence-unit>

But I don't think the problem is here. I did a little bit of debug research and found out the following:

JndiServiceImpl#locate(String jndiName) creates an initial context which differs.

4.3.0 - org.apache.naming.NamingContext

4.2.7 - org.apache.naming.SelectorContext

Except for this, I couldn't find any more differences.

I found some similar topics for this issue, but none of help. Thanks for any help.

Yea, its actually buggy, because they took different focus on how a session-factory works and when the factory is asked for a connection.

So you need to resolve it behind the session-factory to use it when the session-factory requests a fresh connection.

Try to solve the DataSource in jndi in sole respolsibility of hibernate, via the old-scool hibernate.cfg.xml like this:

<hibernate-configuration>
    <session-factory name="data">
        <property name="connection.datasource">java:comp/env/jdbc/data</property>
    ...

And resolve the config via persistence.xml like this:

<persistence version="2.0">
    <persistence-unit name="data">
        <properties>
            <property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/>

Maybe your hbm2ddl must move to hibernate.cfg.xml too then.

Have fun.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM