JTA事务总结(二)

自:http://blog.sina.com.cn/s/blog_661a3fce0100msjb.html
记得EJB的部署文件的事务属性<trans-attribute>RequiresNew</trans-attribute>的情况,在调用该EJB函数时如果已经存在一个事务进行中,那么要求容器挂起该事务,启动新的事务进行函数调用,直到函数结束后,再恢复原来的事务继续进行。

也许你会想到用以下的方式进行:

UserTransaction tx = (UserTransaction)ctx.lookup("javax.transaction.UserTransaction");
UserTransaction tx2 = (UserTransaction)ctx.lookup("javax.transaction.UserTransaction");

tx.begin();

tx2.begin();

tx2.commit();

tx.commit();

以上代码会抛出如下异常:
javax.transaction.NotSupportedException: Another transaction is associated with this thread.

查找了sun的 JTA specification 说明如下:
The UserTransaction.begin method starts a global transaction and associates the transaction with the calling thread. The transaction-to-thread association is managed transparently by the Transaction Manager. Support for nested tranactions is not required. The UserTransaction.begin method throws the NotSupportedException when the calling thread is already associated with a transaction and the transaction manager implementation does not support nested transactions.

看来weblogic的Transaction没有实现嵌套功能,那么容器如何RequiresNew的ejb事务情况呢,就得借助于TransactionManager类了

tm = (TransactionManager)ctx.lookup("javax.transaction.TransactionManager");
tx = (UserTransaction)ctx.lookup("javax.transaction.UserTransaction");
tx.begin();
...
transaction = tm.suspend();
doNestedTransaction();
tm.resume(transaction);
...
tx.commit();

其实以上问题的顺利解决都归功于sun完整的官方资料,想想从事Java快两年了我的大部分知识都是来源于“东拼西凑”:书籍、论坛、项目...看来是该花点时间好好读读sun网站的各种specification了。

http://java.sun.com/products/jta/index.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值