@Override public void afterCompletion(int status) { switch (status) { case Status.STATUS_COMMITTED: try { TransactionSynchronizationUtils.invokeAfterCommit(this.synchronizations); } finally { TransactionSynchronizationUtils.invokeAfterCompletion( this.synchronizations, TransactionSynchronization.STATUS_COMMITTED); } break; case Status.STATUS_ROLLEDBACK: TransactionSynchronizationUtils.invokeAfterCompletion( this.synchronizations, TransactionSynchronization.STATUS_ROLLED_BACK); break; default: TransactionSynchronizationUtils.invokeAfterCompletion( this.synchronizations, TransactionSynchronization.STATUS_UNKNOWN); } }
@Override public void initialize() { // Set sessionTransacted=true in case of a non-JTA transaction manager. if (!this.sessionTransactedCalled && this.transactionManager instanceof ResourceTransactionManager && !TransactionSynchronizationUtils.sameResourceFactory( (ResourceTransactionManager) this.transactionManager, getConnectionFactory())) { super.setSessionTransacted(true); } // Use bean name as default transaction name. if (this.transactionDefinition.getName() == null) { this.transactionDefinition.setName(getBeanName()); } // Proceed with superclass initialization. super.initialize(); }
public void afterCompletion(int status) { switch (status) { case Status.STATUS_COMMITTED: try { TransactionSynchronizationUtils.invokeAfterCommit(this.synchronizations); } finally { TransactionSynchronizationUtils.invokeAfterCompletion( this.synchronizations, TransactionSynchronization.STATUS_COMMITTED); } break; case Status.STATUS_ROLLEDBACK: TransactionSynchronizationUtils.invokeAfterCompletion( this.synchronizations, TransactionSynchronization.STATUS_ROLLED_BACK); break; default: TransactionSynchronizationUtils.invokeAfterCompletion( this.synchronizations, TransactionSynchronization.STATUS_UNKNOWN); } }
@Override public void rollback(TransactionStatus status) throws TransactionException { if(logger.isWarnEnabled()){ logger.warn("Rollback all connected transactions."); } try{ TransactionSynchronizationUtils.triggerBeforeCompletion(); Map<DataSource, ConnectionHolder> connSet = RoutingSynchronizationManager.getSynchronizations(); Exception rollbackException = null; ConnectionHolder rollbackExceptionConnection = null; for (ConnectionHolder connection:connSet.values()) { try { connection.rollback(); if (logger.isDebugEnabled()) { logger.debug("Connection["+ connection +"] has been rolled back."); } } catch (Exception ex) { if (rollbackException == null) { rollbackException = ex; rollbackExceptionConnection = connection; } else { logger.warn("Rollback exception (" + rollbackExceptionConnection + ") " + ex.getMessage(), ex); } } } if (rollbackException != null) { throw new UnexpectedRollbackException("Rollback exception, originated at ("+rollbackExceptionConnection+") "+ rollbackException.getMessage(), rollbackException); } }finally{ RoutingSynchronizationManager .invokeAfterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK); RoutingSynchronizationManager.clearSynchronization(); } }
@Test public void incrementBy1Transactional() throws Exception { // Given TransactionSynchronizationManager.initSynchronization(); // When batchMetrics.increment("counter.test", 1L); TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED); // Then TransactionSynchronizationManager.clearSynchronization(); }
@Test public void decrementBy1Transactional() throws Exception { // Given TransactionSynchronizationManager.initSynchronization(); // When batchMetrics.decrement("counter.test", 1L); TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED); // Then TransactionSynchronizationManager.clearSynchronization(); }
@Test public void submitTransactional() throws Exception { // Given TransactionSynchronizationManager.initSynchronization(); // When batchMetrics.submit("counter.test", 1L); TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED); // Then TransactionSynchronizationManager.clearSynchronization(); }
@Override public void flush() { TransactionSynchronizationUtils.triggerFlush(); }
/** * */ @SuppressWarnings("rawtypes") @Test public void testRegisterTxHandler() { TransactionSynchronizationManager.initSynchronization(); handler.registerTxHandler("entityType", 1L); TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED); ArgumentCaptor<Message> mac = ArgumentCaptor.forClass(Message.class); verify(asyncChannel).send(mac.capture()); Message<?> m = mac.getValue(); assertTrue(m.getPayload() instanceof CommitSuccessEvent); CommitSuccessEvent cse = (CommitSuccessEvent) m.getPayload(); Object id = ((Object[]) cse.getSource())[0]; assertEquals(1L, id); }
public static void invokeAfterCompletion(int completionStatus) { List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager .getSynchronizations(); TransactionSynchronizationUtils.invokeAfterCompletion(synchronizations, completionStatus); }
/** * This implementation triggers flush callbacks, * assuming that they will flush all affected ORM sessions. * @see org.springframework.transaction.support.TransactionSynchronization#flush() */ @Override public void flush() { TransactionSynchronizationUtils.triggerFlush(); }
/** * This implementation triggers flush callbacks, * assuming that they will flush all affected ORM sessions. * @see org.springframework.transaction.support.TransactionSynchronization#flush() */ public void flush() { TransactionSynchronizationUtils.triggerFlush(); }