我有一个直接使用 Java Transaction API 资源的 hibernate 应用程序。我正在尝试使用 UserTransaction 来完成一些基本的数据库事务
UserTransaction 接口定义了允许
应用程序显式管理事务边界
在 hibernate.cfg.xml 文件中,我具有以下属性:
<session-factory>
<property name="show_sql">true</property>
<property name="connection.datasource">java:comp/env/myDataSource</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
在应用程序的 web.xml 中,UserTransaction 设置在<resource-env-ref>
<resource-env-ref>
<description>Object factory for MyBean instances.</description>
<resource-env-ref-name>UserTransaction</resource-env-ref-name>
<resource-env-ref-type>javax.transaction.UserTransaction</resource-env-ref-type>
</resource-env-ref>
在代码中,我尝试使用以下命令查找 UserTransactionInitialContext
by its JNDI name
public UserTransaction getTransactionContext()
throws Exception
{
if (this.ut == null) {
ut = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");
}
return ut;
}
但是返回以下异常:
03:27:37,530 ERROR [someServlet:555] Error in someMethod
javax.naming.NamingException: Cannot create resource instance
at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:117)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
该应用程序从正常运行的 Weblogic 服务器移植到 Apache Tomcat;话虽这么说-我需要定义吗用户交易 in my context.xml
为了getTransactionContext()
找到它?如果不是,我怎样才能在我的代码中正确返回 UserTransaction ?