1 10 11 package org.mule.transaction; 12 13 import java.beans.ExceptionListener ; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.mule.config.i18n.Message; 18 import org.mule.config.i18n.Messages; 19 import org.mule.umo.UMOTransaction; 20 import org.mule.umo.UMOTransactionConfig; 21 22 public class TransactionTemplate 23 { 24 private static final Log logger = LogFactory.getLog(TransactionTemplate.class); 25 26 private final UMOTransactionConfig config; 27 private final ExceptionListener exceptionListener; 28 29 public TransactionTemplate(UMOTransactionConfig config, ExceptionListener listener) 30 { 31 this.config = config; 32 exceptionListener = listener; 33 } 34 35 public Object execute(TransactionCallback callback) throws Exception 36 { 37 if (config == null) 38 { 39 return callback.doInTransaction(); 40 } 41 else 42 { 43 byte action = config.getAction(); 44 UMOTransaction tx = TransactionCoordination.getInstance().getTransaction(); 45 46 if (action == UMOTransactionConfig.ACTION_NONE && tx != null) 47 { 48 throw new IllegalTransactionStateException(new Message(Messages.TX_AVAILABLE_BUT_ACTION_IS_X, 49 "None")); 50 } 51 else if (action == UMOTransactionConfig.ACTION_ALWAYS_BEGIN && tx != null) 52 { 53 throw new IllegalTransactionStateException(new Message(Messages.TX_AVAILABLE_BUT_ACTION_IS_X, 54 "Always Begin")); 55 } 56 else if (action == UMOTransactionConfig.ACTION_ALWAYS_JOIN && tx == null) 57 { 58 throw new IllegalTransactionStateException(new Message( 59 Messages.TX_NOT_AVAILABLE_BUT_ACTION_IS_X, "Always Join")); 60 } 61 62 if (action == UMOTransactionConfig.ACTION_ALWAYS_BEGIN 63 || action == UMOTransactionConfig.ACTION_BEGIN_OR_JOIN) 64 { 65 logger.debug("Beginning transaction"); 66 tx = config.getFactory().beginTransaction(); 67 logger.debug("Transaction successfully started"); 68 } 69 else 70 { 71 tx = null; 72 } 73 try 74 { 75 Object result = callback.doInTransaction(); 76 if (tx != null) 77 { 78 if (tx.isRollbackOnly()) 79 { 80 logger.debug("Transaction is marked for rollback"); 81 tx.rollback(); 82 } 83 else 84 { 85 logger.debug("Committing transaction"); 86 tx.commit(); 87 } 88 } 89 return result; 90 } 91 catch (Exception e) 92 { 93 if (exceptionListener != null) 94 { 95 logger 96 .info("Exception Caught in Transaction template. Handing off to exception handler: " 97 + exceptionListener); 98 exceptionListener.exceptionThrown(e); 99 } 100 else 101 { 102 logger 103 .info("Exception Caught in Transaction template without any exception listeners defined, exception is rethrown."); 104 if (tx != null) 105 { 106 tx.setRollbackOnly(); 107 } 108 } 109 if (tx != null) 110 { 111 if (tx.isRollbackOnly()) 119 { 120 logger.debug("Exception caught: rollback transaction", e); 121 tx.rollback(); 122 } 123 else 124 { 125 tx.commit(); 126 } 127 } 128 if (exceptionListener != null) 130 { 131 return null; 132 } 133 else 134 { 135 throw e; 136 } 137 } 138 catch (Error e) 139 { 140 if (tx != null) 141 { 142 logger.info("Error caught: rollback transaction", e); 143 tx.rollback(); 144 } 145 throw e; 146 } 147 } 148 } 149 150 } 151 | Popular Tags |