JTA使用心得

JTA是一种J2EE分布式事务框架,用于支持多数据源的分布式事务。

1、使用JTA,在应用服务器中配置的数据源必须是XA类型的,否则JTA不生效。

2、各大应用服务器都有JTA的实现。但tomcat不支持。

3、今天遇到了事务在异步情况下的问题。如果事务中的一部分在是在独立的线程中运行,那么。。。。目前还没找到好的办法,不知道是否是无解的。

下面的这些代码片断支持嵌套JTA事务

启动事务:判断事务如果是已经被激活的,就不启动新的事务


  1. public   static   boolean  begin()  throws  TransactionException {  
  2.     UserTransaction ut = TransactionFactory.getUserTransaction();  
  3.   
  4.     if  (ut !=  null ) {  
  5.         try  {  
  6.             if  (ut.getStatus() == TransactionUtil.STATUS_ACTIVE) {  
  7.                 Debug.debug("[TransactionUtil.begin] active transaction in place, so no transaction begun" , module);  
  8.                 return   false ;  
  9.             }  
  10.             ut.begin();  
  11.             //Debug.debug("[TransactionUtil.begin] transaction begun", module);   
  12.             return   true ;  
  13.         } catch  (NotSupportedException e) {  
  14.             //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();   
  15.             throw   new  TransactionException( "Not Supported error, could not begin transaction (probably a nesting problem)" , e);  
  16.         } catch  (SystemException e) {  
  17.             //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();   
  18.             throw   new  TransactionException( "System error, could not begin transaction" , e);  
  19.         }  
  20.     } else  {  
  21.         Debug.debug("[TransactionUtil.begin] no user transaction, so no transaction begun" , module);  
  22.         return   false ;  
  23.     }  

public static boolean begin() throws TransactionException { UserTransaction ut = TransactionFactory.getUserTransaction(); if (ut != null) { try { if (ut.getStatus() == TransactionUtil.STATUS_ACTIVE) { Debug.debug("[TransactionUtil.begin] active transaction in place, so no transaction begun", module); return false; } ut.begin(); //Debug.debug("[TransactionUtil.begin] transaction begun", module); return true; } catch (NotSupportedException e) { //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause(); throw new TransactionException("Not Supported error, could not begin transaction (probably a nesting problem)", e); } catch (SystemException e) { //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause(); throw new TransactionException("System error, could not begin transaction", e); } } else { Debug.debug("[TransactionUtil.begin] no user transaction, so no transaction begun", module); return false; }

事务提交:


  1. public   static   void  commit( boolean  beganTransaction)  throws  TransactionException {  
  2.         if  (beganTransaction)  
  3.             TransactionUtil.commit();  
  4.     }  
  5. public   static   void  commit()  throws  TransactionException {  
  6.         UserTransaction ut = TransactionFactory.getUserTransaction();  
  7.   
  8.         if  (ut !=  null ) {  
  9.             try  {  
  10.                 int  status = ut.getStatus();  
  11.   
  12.                 if  (status != STATUS_NO_TRANSACTION) {  
  13.                     ut.commit();  
  14.                     //Debug.debug("[TransactionUtil.commit] transaction committed", module);   
  15.                 } else  {  
  16.                     //十分影响性能,因为引擎中 遗留着非常多查询操作进行事务提交的代码   
  17.                     //Debug.debug("[TransactionUtil.commit] Not committing transaction, status is STATUS_NO_TRANSACTION", module);   
  18.                 }  
  19.             } catch  (RollbackException e) {  
  20.                 //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();   
  21.                 throw   new  TransactionException( "Roll back error, could not commit transaction, was rolled back instead" , e);  
  22.             } catch  (HeuristicMixedException e) {  
  23.                 //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();   
  24.                 throw   new  TransactionException( "Could not commit transaction, HeuristicMixed exception" , e);  
  25.             } catch  (HeuristicRollbackException e) {  
  26.                 //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();   
  27.                 throw   new  TransactionException( "Could not commit transaction, HeuristicRollback exception" , e);  
  28.             } catch  (SystemException e) {  
  29.                 //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();   
  30.                 throw   new  TransactionException( "System error, could not commit transaction" , e);  
  31.             }  
  32.         } else  {  
  33.               
  34.             Debug.debug("[TransactionUtil.commit] UserTransaction is null, not commiting" , module);  
  35.         }  
  36.     }  

public static void commit(boolean beganTransaction) throws TransactionException { if (beganTransaction) TransactionUtil.commit(); } public static void commit() throws TransactionException { UserTransaction ut = TransactionFactory.getUserTransaction(); if (ut != null) { try { int status = ut.getStatus(); if (status != STATUS_NO_TRANSACTION) { ut.commit(); //Debug.debug("[TransactionUtil.commit] transaction committed", module); } else { //十分影响性能,因为引擎中遗留着非常多查询操作进行事务提交的代码 //Debug.debug("[TransactionUtil.commit] Not committing transaction, status is STATUS_NO_TRANSACTION", module); } } catch (RollbackException e) { //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause(); throw new TransactionException("Roll back error, could not commit transaction, was rolled back instead", e); } catch (HeuristicMixedException e) { //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause(); throw new TransactionException("Could not commit transaction, HeuristicMixed exception", e); } catch (HeuristicRollbackException e) { //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause(); throw new TransactionException("Could not commit transaction, HeuristicRollback exception", e); } catch (SystemException e) { //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause(); throw new TransactionException("System error, could not commit transaction", e); } } else { Debug.debug("[TransactionUtil.commit] UserTransaction is null, not commiting", module); } }

使用事务:


  1. public   void  changeState(String newState)  throws  fException, InvalidState, TransitionNotAllowed {  
  2.     Connection conn = null ;  
  3.     boolean  beganTransaction =  false ;  
  4.     try  {  
  5.     beganTransaction = TransactionUtil.begin();  
  6.     conn = TransactionUtil.getConnection();  
  7.           //一堆数据库操作   
  8.          TransactionUtil.commit(beganTransaction);  
  9.         } catch  (DAOException e) {  
  10.             TransactionUtil.rollbackbeganTransaction);  
  11.         } catch  (TransactionException e) {  
  12.         TransactionUtil.rollbac(beganTransaction);  
  13.         } finally  {  
  14.         TransactionUtil.closeConnection(conn);  
  15.         }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值