在使用hibernate的jta事务管理时,为什么提示我javax.naming.NameNotFoundException?
我在使用hibernate时,想使用JTA来进行事务管理,可是程序运行的时候老是提示我
javax.naming.NameNotFoundException: Name javax.transaction.UserTransaction is not bound in this Context
如果用jdbc的transaction就可以,不知道用JTA是不是还要设置什么地方?
hibernate.cfg.xml配置如下:
<hibernate-configuration>
<session-factory>
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
<property name="connection.datasource">java:comp/env/jdbc/aa</property>
<property name="show_sql">false</property>
<property name="transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property>
<property name="jta.UserTransaction">javax.transaction.UserTransaction</property>
<!-- Mapping files -->
<!-- Attend Mdl -->
<mapping resource="com/test/Module.hbm.xml"/>
</session-factory>
</hibernate-configuration>
程序代码如下:
public void testJta() throws Exception {
UserTransaction tx = null;
try {
tx = (UserTransaction)new InitialContext().lookup(
"javax.transaction.UserTransaction");
tx.begin();
addOne();//插入一条记录
tx.commit();
}
catch (Exception e) {
e.printStackTrace();
if (tx != null)
try {
tx.rollback();
}
catch (SystemException ex) {
}
}
}