| public class CacheTransactionTemplate extends TransactionTemplate { private static final long serialVersionUID = 489621858441822688L; private boolean cacheable = true; private boolean isInitialized = false; /* (non-Javadoc) * @see org.springframework.transaction.support.TransactionTemplate#afterPropertiesSet() */ @Override public void afterPropertiesSet() { super.afterPropertiesSet(); isInitialized = true; } /* (non-Javadoc) * @see org.springframework.transaction.support.TransactionTemplate#execute(org.springframework.transaction.support.TransactionCallback) */ @Override public Object execute(TransactionCallback action) throws TransactionException { TransactionStatus status = getTransactionManager().getTransaction(this); initialCacheModel(status); Object result = null; try { result = action.doInTransaction(status); logger.debug(action); } catch (RuntimeException ex) { // transactional code threw application exception -> rollback rollbackOnException(status, ex); throw ex; } catch (Error err) { // transactional code threw error -> rollback rollbackOnException(status, err); throw err; } try { getTransactionManager().commit(status); } finally { destoryCacheModel(status); } return result; } /** * Perform a rollback, handling rollback exceptions properly. * * @param status object redivsenting the transaction * * @param ex the thrown application exception or error * * @throws TransactionException in case of a rollback error */ private void rollbackOnException(TransactionStatus status, Throwable ex) throws TransactionException { logger.debug("Initiating transaction rollback on application exception", ex); try { getTransactionManager().rollback(status); } catch (RuntimeException ex2) { logger.error("Application exception overridden by rollback exception", ex); throw ex2; } catch (Error err) { logger.error("Application exception overridden by rollback error", ex); throw err; } finally { destoryCacheModel(status); } } /** * Initail current transaction's CacheModel. * * <p> * Must be a new Transaction. * * Current transaction's CacheModel must have not been initialized. * </p> * * @param status */ private void initialCacheModel(TransactionStatus status) { if (cacheable && status != null && status.isNewTransaction()) { if (!CacheModel.exist()) { CacheModel.initial(); } } } /** * Destroy current transaction's CacheModel. * * <p> * Must |