1 18 package org.apache.activemq.util; 19 20 import java.io.IOException ; 21 import org.apache.activemq.broker.ConnectionContext; 22 import org.apache.activemq.store.PersistenceAdapter; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 32 public class TransactionTemplate { 33 static private final Log log=LogFactory.getLog(TransactionTemplate.class); 34 private PersistenceAdapter persistenceAdapter; 35 private ConnectionContext context; 36 37 public TransactionTemplate(PersistenceAdapter persistenceAdapter, ConnectionContext context) { 38 this.persistenceAdapter = persistenceAdapter; 39 this.context=context; 40 } 41 42 public void run(Callback task) throws IOException { 43 persistenceAdapter.beginTransaction(context); 44 Throwable throwable = null; 45 try { 46 task.execute(); 47 } 48 catch (IOException t) { 49 throwable = t; 50 throw t; 51 } 52 catch (RuntimeException t) { 53 throwable = t; 54 throw t; 55 } 56 catch (Throwable t) { 57 throwable = t; 58 throw IOExceptionSupport.create("Persistence task failed: "+t, t); 59 } finally { 60 if (throwable == null) { 61 persistenceAdapter.commitTransaction(context); 62 } 63 else { 64 log.error("Having to Rollback - caught an exception: " + throwable); 65 persistenceAdapter.rollbackTransaction(context); 66 } 67 } 68 } 69 70 public ConnectionContext getContext() { 71 return context; 72 } 73 74 public PersistenceAdapter getPersistenceAdapter() { 75 return persistenceAdapter; 76 } 77 } 78 | Popular Tags |