JTA是一种J2EE分布式事务框架,用于支持多数据源的分布式事务。
1、使用JTA,在应用服务器中配置的数据源必须是XA类型的,否则JTA不生效。
2、各大应用服务器都有JTA的实现。但tomcat不支持。
3、今天遇到了事务在异步情况下的问题。如果事务中的一部分在是在独立的线程中运行,那么。。。。目前还没找到好的办法,不知道是否是无解的。
下面的这些代码片断支持嵌套JTA事务
启动事务:判断事务如果是已经被激活的,就不启动新的事务
- 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 ;
- }
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; }
事务提交:
- 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);
- }
- }
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); } }
使用事务:
- public void changeState(String newState) throws fException, InvalidState, TransitionNotAllowed {
- Connection conn = null ;
- boolean beganTransaction = false ;
- try {
- beganTransaction = TransactionUtil.begin();
- conn = TransactionUtil.getConnection();
- //一堆数据库操作
- TransactionUtil.commit(beganTransaction);
- } catch (DAOException e) {
- TransactionUtil.rollbackbeganTransaction);
- } catch (TransactionException e) {
- TransactionUtil.rollbac(beganTransaction);
- } finally {
- TransactionUtil.closeConnection(conn);
- }
- }