使用DataSource抛出HibernatePersistence javax.naming.NameNotFoundException

时间:2015-06-18 13:30:49

标签: spring hibernate tomcat tomcat7

我正在使用带有jta数据源的Hibernate构建Tomcat Servlet应用程序。我的persistence.xml包含以下内容:

 <persistence version="2.0" 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">
     <persistence-unit name="metadata.model" transaction-type="JTA">
     <provider>org.hibernate.ejb.HibernatePersistence</provider>
     <jta-data-source>jdbc/BitronixJTADataSource</jta-data-source>

...
</persistence>

在我的${web-app}/WebContent/META-INF/Context.xml中,我有以下内容:

<Context>
<Resource name="jdbc/BitronixJTADataSource" auth="Container"
           type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="root" password="mysecretpassword"
           driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/metadatadb" />
</Context>

在应用程序启动后,我发出:

emf = Persistence.createEntityManagerFactory("metadata.model");
entityManager = emf.createEntityManager();

并获得

javax.naming.NameNotFoundException: Name [jdbc/BitronixJTADataSource] is not bound in this Context. Unable to find [jdbc].

你知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

似乎在persistence.xml中,jta-data-source应该具有java:comp/env/jdbc/BitronixJTADataSource值,以便在InitialContext中找到它。