Hibernate 4.3 + Tomcat 7 Unable to find JNDI name

I have a problem 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 am using Tomcat 7.0.29 (example 7.0.47) and JDK 7 (v25). 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 do not think the problem is here. I did a little debugging research and found out the following:

JndiServiceImpl#locate(String jndiName) creates an original context that is different.

4.3.0 - org.apache.naming.NamingContext

4.2.7 - org.apache.naming.SelectorContext

Other than that, I could not find any more differences.

I found several similar topics for this problem, but none of them helped. Thanks for any help.

+6
source share
1 answer

Yes, these are actually buggies because they focused on how session-factory works and when a factory request is requested for a connection.

So, you need to enable it for session-factory in order to use it when session-factory requests a new connection.

Try to solve the DataSource in jndi in a single hibernation volatility via old-scool hibernate.cfg.xml as follows:

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

And enable the configuration using persistence.xml as follows:

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

Perhaps your hbm2ddl should also move to hibernate.cfg.xml .

Good luck.

0
source

Source: https://habr.com/ru/post/958607/


All Articles