首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

JTA事宜总结(二)

2012-09-02 
JTA事务总结(二)自:http://blog.sina.com.cn/s/blog_661a3fce0100msjb.html记得EJB的部署文件的事务属性t

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

热点排行