1 17 package org.alfresco.repo.transaction; 18 19 import javax.transaction.RollbackException ; 20 import javax.transaction.Status ; 21 import javax.transaction.UserTransaction ; 22 23 import org.alfresco.error.AlfrescoRuntimeException; 24 import org.alfresco.service.transaction.TransactionService; 25 import org.alfresco.util.ParameterCheck; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 34 public class TransactionUtil 35 { 36 private static Log logger = LogFactory.getLog(TransactionUtil.class); 37 38 44 public interface TransactionWork<Result> 45 { 46 51 Result doWork() throws Exception ; 52 } 53 54 57 public static void flush() 58 { 59 AlfrescoTransactionSupport.flush(); 60 } 61 62 70 public static <R> R executeInUserTransaction( 71 TransactionService transactionService, 72 TransactionWork<R> transactionWork) 73 { 74 return executeInTransaction(transactionService, transactionWork, false, false); 75 } 76 77 87 public static <R> R executeInUserTransaction( 88 TransactionService transactionService, 89 TransactionWork<R> transactionWork, 90 boolean readOnly) 91 { 92 return executeInTransaction(transactionService, transactionWork, false, readOnly); 93 } 94 95 104 public static <R> R executeInNonPropagatingUserTransaction( 105 TransactionService transactionService, 106 TransactionWork<R> transactionWork) 107 { 108 return executeInTransaction(transactionService, transactionWork, true, false); 109 } 110 111 121 public static <R> R executeInNonPropagatingUserTransaction( 122 TransactionService transactionService, 123 TransactionWork<R> transactionWork, 124 boolean readOnly) 125 { 126 return executeInTransaction(transactionService, transactionWork, true, readOnly); 127 } 128 129 142 private static <R> R executeInTransaction( 143 TransactionService transactionService, 144 TransactionWork<R> transactionWork, 145 boolean nonPropagatingUserTransaction, 146 boolean readOnly) 147 { 148 ParameterCheck.mandatory("transactionWork", transactionWork); 149 150 R result = null; 151 152 UserTransaction txn = null; 154 if (nonPropagatingUserTransaction == true) 155 { 156 txn = transactionService.getNonPropagatingUserTransaction(); 157 } 158 else 159 { 160 txn = transactionService.getUserTransaction(readOnly); 161 } 162 163 try 164 { 165 txn.begin(); 168 result = transactionWork.doWork(); 169 if (txn.getStatus() == Status.STATUS_MARKED_ROLLBACK) 171 { 172 txn.rollback(); 174 } 175 else 176 { 177 txn.commit(); 179 } 180 } 181 catch (RollbackException exception) 182 { 183 throw new AlfrescoRuntimeException( 185 "Unexpected rollback of exception: \n" + exception.getMessage(), 186 exception); 187 } 188 catch (Throwable exception) 189 { 190 try 191 { 192 txn.rollback(); 194 } 195 catch (Throwable rollbackException) 196 { 197 logger.error("Error rolling back transaction", rollbackException); 199 } 200 201 if (exception instanceof RuntimeException ) 203 { 204 throw (RuntimeException ) exception; 205 } 206 else 207 { 208 throw new RuntimeException ("Error during execution of transaction.", exception); 209 } 210 } 211 212 return result; 213 } 214 } | Popular Tags |