在使用persistence.xml时将Hibernate 4.0与Tomcat 7结合使用时发生JndiException [英] JndiException when using Hibernate 4.0 with Tomcat 7 when using persistence.xml
问题描述
我在Tomcat 7上使用Hibernate 4.0和JPA persistence.xml文件。没有Struts,只是将Hibernate与一些Jersey服务直接相连。这是我遇到的异常:
引起:org.hibernate.service.jndi.JndiException:无法查找JNDI名称[jdbc / MyDB]
位于org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:68)
位于org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl .configure(DatasourceConnectionProviderImpl.java:116)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl .java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java :223)
在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)
在org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate .service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
at org.hibernate.cfg.Configuration.buildSettingsInternal (Configuration.java:2273)
在org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2269)
在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1738)
at org.hibernate.ejb.EntityManagerFactoryImpl。< init>(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
.. 。8 more
引起:javax.naming.NameNotFoundException:名称jdbc是不在此背景下绑定
在org.apache.naming.NamingContext.lookup(NamingContext.java:820)
在org.apache.naming.NamingContext.lookup(NamingContext.java:154)
在org.apache.naming.SelectorContext.lookup(SelectorContext.java:135)
在javax.naming.InitialContext.lookup(InitialContext.java:396)
在org.hibernate.service.jndi。 internal.JndiServiceImpl.locate(JndiServiceImpl.java:65)
... 23 more
我看到关于jbc的说明在这种情况下不受约束,但我很困惑这是如何发生的。我在下面的app-specific context.xml中部署我的上下文:
<?xml version ='1.0'编码= 'UTF-8' >?;
<上下文>
< Resource name =jdbc / MyDBauth =Containertype =javax.sql.DataSource
maxActive =100maxIdle =30maxWait =10000
username =...password =...driverClassName =com.mysql.jdbc.Driver
url =jdbc:mysql:// localhost:3306 / mydb/>
< / Context>
我的persistence.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 =com.example.mysqltransaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
<非-jta-data-source> jdbc / MyDB< / non-jta-data-source>
< class> ...< / class>
<属性>
< property name =hibernate.connection.datasourcevalue =jdbc / MyDB/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< property name =hibernate.id.new_generator_mappingsvalue =true/>
< / properties>
< / persistence-unit>
< /余辉>
最后,我的web.xml文件中定义的资源如下所示:
<!DOCTYPE web-app PUBLIC
- // Sun Microsystems,Inc. //DTD Web Application 2.3 // EN
http://java.sun.com/dtd/web-app_2_3.dtd>
< web-app>
< display-name>我的Web应用程序< / display-name>
< resource-ref>
< description> DB Connection< / description>
< res-ref-name> jdbc / MyDB< / res-ref-name>
< res-type> javax.sql.DataSource< / res-type>
< res-auth>容器< / res-auth>
< / resource-ref>
...
< / web-app>
至于我的布局,以下是我的war文件的结构:
app.war
+ META-INF
- context.xml
+ WEB-INF
+类
+ META-INF
- persistence.xml
+ lib
- web.xml
一些小的注释:
- 使用全局上下文而不是特定于应用程序的上下文没有区别。
- 试图实例化一个EntityManager实例的代码位于lib目录(多项目Maven构建的一部分)中的JAR文件中,但持久性XML在主Web应用程序中作为
- 我可以在Tomcat中看到JNDI数据源,我可以使用 psi-probe ,即我可以访问连接信息,并成功地对数据源执行SQL查询。 =因为你使用了一个可移植的资源,你应该使用java:comp / env / your_resource来调用你的JNDI,比如java:comp / env / jdbc / MyDB
I'm using Hibernate 4.0 with a JPA persistence.xml file on Tomcat 7. No Struts, just straight Hibernate with some Jersey services. Here is the exception I'm running into:
Caused by: org.hibernate.service.jndi.JndiException: Unable to lookup JNDI name [jdbc/MyDB]
at org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:68)
at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:116)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)
at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2273)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2269)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1738)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
... 8 more
Caused by: javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:135)
at javax.naming.InitialContext.lookup(InitialContext.java:396)
at org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:65)
... 23 more
I see the note about jbc is not bound in this context, but I'm confused as how this is happening. I'm deploying my context in an app-specific context.xml, below:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/MyDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="..." password="..." driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb"/>
</Context>
And my persistence.xml file looks like:
<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="com.example.mysql" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>jdbc/MyDB</non-jta-data-source>
<class>...</class>
<properties>
<property name="hibernate.connection.datasource" value="jdbc/MyDB"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.id.new_generator_mappings" value ="true"/>
</properties>
</persistence-unit>
</persistence>
Finally, my web.xml file has the resource defined as so:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>My Web Application</display-name>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/MyDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
...
</web-app>
As for my layout, here is how my war file is structured:
app.war
+ META-INF
- context.xml
+ WEB-INF
+ classes
+ META-INF
- persistence.xml
+ lib
- web.xml
A few minor notes:
- Using a global context versus an application-specific context makes no difference.
- The code trying to instantiate an EntityManager instance is in a JAR file in the lib directory (part of a multi-project Maven build), but the persistence XML is in the main web app as outlined above.
- I can see the JNDI datasource in Tomcat and I can query it using psi-probe, i.e. I can access the connection information and successfully execute SQL queries against the data source.
Since you use a portable resource with , you should call your JNDI using "java:comp/env/your_resource", like java:comp/env/jdbc/MyDB
这篇关于在使用persistence.xml时将Hibernate 4.0与Tomcat 7结合使用时发生JndiException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!