Tomcat 使用用户事务的 Hibernate 持久性
Hibernate Persistence in Tomcat using User Transaction
当我使用以下配置时出现异常 Name [jdbc/JAWSDB] is not bound in this Context. Unable to find [jdbc]
。我还添加了所有必需的设置,如下所示。
持久性如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence_2_1.xsd">
<persistence-unit name="MyPresistance">
<jta-data-source>jdbc/JAWSDB</jta-data-source>
<class>com.hibernate.model.Message</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
</properties>
</persistence-unit>
</persistence>
控制器使用持久性文件加载实体管理器:
@GET
@Path("/{name}")
public Response sayHello(@PathParam("name") String msg) {
String output = "Hello, " + msg + "!";
Context initContext;
try {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPresistance");
initContext = new InitialContext();
UserTransaction tx = (UserTransaction)initContext.lookup("java:comp/UserTransaction");
tx.begin();
EntityManager em = emf.createEntityManager();
Message message = new Message();
message.setText("Hello World!");
em.persist(message);
tx.commit();
// INSERT into MESSAGE (ID, TEXT) values (1, 'Hello World!')
em.close();
}
}
Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- url: The JDBC connection url for connecting to your MySQL database. -->
<Resource name="jdbc/JAWSDB" auth="Container"
type="javax.sql.DataSource" maxTotal="100" maxIdle="30"
maxWaitMillis="10000" username="root"
password="root" driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost/test" />
<Resource name="UserTransaction" auth="Container" type="javax.transaction.UserTransaction"/>
<Transaction factory="org.objectweb.jotm.UserTransactionFactory" jotm.timeout="60"/>
</Context>
Web.xml :
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- Define ServletContainer of Jersey -->
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Define the ResourceConfig class -->
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.hibernate.app.Application</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all the URLs to the Jersey ServletContainer -->
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/JAWSDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
你能告诉我我在这里遗漏了什么吗?我觉得从上下文资源加载用户事务并没有将数据库资源注入其中,因为它在上下文中作为单独的资源保存。
添加了异常堆栈跟踪。
javax.naming.NameNotFoundException: Name [jdbc/JAWSDB] is not bound in this Context. Unable to find [jdbc].
org.apache.naming.NamingContext.lookup(NamingContext.java:817)
org.apache.naming.NamingContext.lookup(NamingContext.java:159)
org.apache.naming.SelectorContext.lookup(SelectorContext.java:140)
javax.naming.InitialContext.lookup(Unknown Source)
org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:97)
org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:98)
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
改变你的persistence.xml
来自
<jta-data-source>jdbc/JAWSDB</jta-data-source>
到
<jta-data-source>java:comp/env/jdbc/JAWSDB</jta-data-source>
再试一次
当我使用以下配置时出现异常 Name [jdbc/JAWSDB] is not bound in this Context. Unable to find [jdbc]
。我还添加了所有必需的设置,如下所示。
持久性如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence_2_1.xsd">
<persistence-unit name="MyPresistance">
<jta-data-source>jdbc/JAWSDB</jta-data-source>
<class>com.hibernate.model.Message</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.use_sql_comments" value="true" />
</properties>
</persistence-unit>
</persistence>
控制器使用持久性文件加载实体管理器:
@GET
@Path("/{name}")
public Response sayHello(@PathParam("name") String msg) {
String output = "Hello, " + msg + "!";
Context initContext;
try {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPresistance");
initContext = new InitialContext();
UserTransaction tx = (UserTransaction)initContext.lookup("java:comp/UserTransaction");
tx.begin();
EntityManager em = emf.createEntityManager();
Message message = new Message();
message.setText("Hello World!");
em.persist(message);
tx.commit();
// INSERT into MESSAGE (ID, TEXT) values (1, 'Hello World!')
em.close();
}
}
Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- url: The JDBC connection url for connecting to your MySQL database. -->
<Resource name="jdbc/JAWSDB" auth="Container"
type="javax.sql.DataSource" maxTotal="100" maxIdle="30"
maxWaitMillis="10000" username="root"
password="root" driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost/test" />
<Resource name="UserTransaction" auth="Container" type="javax.transaction.UserTransaction"/>
<Transaction factory="org.objectweb.jotm.UserTransactionFactory" jotm.timeout="60"/>
</Context>
Web.xml :
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- Define ServletContainer of Jersey -->
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Define the ResourceConfig class -->
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.hibernate.app.Application</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all the URLs to the Jersey ServletContainer -->
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/JAWSDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
你能告诉我我在这里遗漏了什么吗?我觉得从上下文资源加载用户事务并没有将数据库资源注入其中,因为它在上下文中作为单独的资源保存。
添加了异常堆栈跟踪。
javax.naming.NameNotFoundException: Name [jdbc/JAWSDB] is not bound in this Context. Unable to find [jdbc].
org.apache.naming.NamingContext.lookup(NamingContext.java:817)
org.apache.naming.NamingContext.lookup(NamingContext.java:159)
org.apache.naming.SelectorContext.lookup(SelectorContext.java:140)
javax.naming.InitialContext.lookup(Unknown Source)
org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:97)
org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:98)
org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
改变你的persistence.xml
来自
<jta-data-source>jdbc/JAWSDB</jta-data-source>
到
<jta-data-source>java:comp/env/jdbc/JAWSDB</jta-data-source>
再试一次