/** * Trigger {@code beforeCommit} callbacks on all currently registered synchronizations. * @param readOnly whether the transaction is defined as read-only transaction * @throws RuntimeException if thrown by a {@code beforeCommit} callback * @see TransactionSynchronization#beforeCommit(boolean) */ public static void triggerBeforeCommit(boolean readOnly) { for (TransactionSynchronization synchronization : TransactionSynchronizationManager.getSynchronizations()) { synchronization.beforeCommit(readOnly); } }
/** * Trigger {@code beforeCommit} callbacks on all currently registered synchronizations. * @param readOnly whether the transaction is defined as read-only transaction * @throws RuntimeException if thrown by a {@code beforeCommit} callback * @see TransactionSynchronization#beforeCommit(boolean) */ public static void triggerBeforeCommit(boolean readOnly) { for (TransactionSynchronization synchronization : TransactionSynchronizationManager.getSynchronizations()) { synchronization.beforeCommit(readOnly); } }
/** * JTA {@code beforeCompletion} callback: just invoked before commit. * <p>In case of an exception, the JTA transaction will be marked as rollback-only. * @see org.springframework.transaction.support.TransactionSynchronization#beforeCommit */ @Override public void beforeCompletion() { try { boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); this.springSynchronization.beforeCommit(readOnly); } catch (RuntimeException | Error ex) { setRollbackOnlyIfPossible(); throw ex; } finally { // Process Spring's beforeCompletion early, in order to avoid issues // with strict JTA implementations that issue warnings when doing JDBC // operations after transaction completion (e.g. Connection.getWarnings). this.beforeCompletionCalled = true; this.springSynchronization.beforeCompletion(); } }
/** * JTA {@code beforeCompletion} callback: just invoked before commit. * <p>In case of an exception, the JTA transaction will be marked as rollback-only. * @see org.springframework.transaction.support.TransactionSynchronization#beforeCommit */ @Override public void beforeCompletion() { try { boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); this.springSynchronization.beforeCommit(readOnly); } catch (RuntimeException | Error ex) { setRollbackOnlyIfPossible(); throw ex; } finally { // Process Spring's beforeCompletion early, in order to avoid issues // with strict JTA implementations that issue warnings when doing JDBC // operations after transaction completion (e.g. Connection.getWarnings). this.beforeCompletionCalled = true; this.springSynchronization.beforeCompletion(); } }
assertEquals(1, synchs.size()); TransactionSynchronization synch = synchs.get(0); synch.beforeCommit(false); synch.beforeCompletion(); synch.afterCommit();
@Test public void jtaTransactionManagerWithExistingTransactionAndCommitException() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); final TransactionSynchronization synch = mock(TransactionSynchronization.class); willThrow(new OptimisticLockingFailureException("")).given(synch).beforeCommit(false); JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); try { tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); TransactionSynchronizationManager.registerSynchronization(synch); } }); fail("Should have thrown OptimisticLockingFailureException"); } catch (OptimisticLockingFailureException ex) { // expected } assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(ut).setRollbackOnly(); verify(synch).beforeCompletion(); verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN); }
@Test public void jtaTransactionManagerWithCommitAndSynchronizationOnActual() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE); final TransactionSynchronization synch = mock(TransactionSynchronization.class); JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { // something transactional assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); TransactionSynchronizationManager.registerSynchronization(synch); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(ut).begin(); verify(ut).commit(); verify(synch).beforeCommit(false); verify(synch).beforeCompletion(); verify(synch).afterCommit(); verify(synch).afterCompletion(TransactionSynchronization.STATUS_COMMITTED); }
verify(synch).beforeCommit(false); verify(synch).beforeCompletion(); verify(synch).afterCommit();
@Override public void beforeCommit(boolean readOnly) { this.synchronization.beforeCommit(readOnly); }
@Override public void beforeCommit(boolean readOnly) { this.synchronization.beforeCommit(readOnly); }
/** * Trigger {@code beforeCommit} callbacks on all currently registered synchronizations. * @param readOnly whether the transaction is defined as read-only transaction * @throws RuntimeException if thrown by a {@code beforeCommit} callback * @see TransactionSynchronization#beforeCommit(boolean) */ public static void triggerBeforeCommit(boolean readOnly) { for (TransactionSynchronization synchronization : TransactionSynchronizationManager.getSynchronizations()) { synchronization.beforeCommit(readOnly); } }
/** * Trigger {@code beforeCommit} callbacks on all currently registered synchronizations. * @param readOnly whether the transaction is defined as read-only transaction * @throws RuntimeException if thrown by a {@code beforeCommit} callback * @see TransactionSynchronization#beforeCommit(boolean) */ public static void triggerBeforeCommit(boolean readOnly) { for (TransactionSynchronization synchronization : TransactionSynchronizationManager.getSynchronizations()) { synchronization.beforeCommit(readOnly); } }
private void publishEntityChangedEvents(List<EntityChangedEvent> collectedEvents) { if (collectedEvents.isEmpty()) return; List<TransactionSynchronization> synchronizationsBefore = new ArrayList<>( TransactionSynchronizationManager.getSynchronizations()); entityChangedEventManager.publish(collectedEvents); List<TransactionSynchronization> synchronizations = new ArrayList<>( TransactionSynchronizationManager.getSynchronizations()); if (synchronizations.size() > synchronizationsBefore.size()) { synchronizations.removeAll(synchronizationsBefore); for (TransactionSynchronization synchronization : synchronizations) { synchronization.beforeCommit(false); } } }
/** * Trigger beforeCommit callbacks. * @param status object representing the transaction */ private void triggerBeforeCommit(DefaultTransactionStatus status) { if (status.isNewSynchronization()) { if (status.isDebug()) { logger.debug("Triggering beforeCommit synchronization"); } for (Iterator it = TransactionSynchronizationManager.getSynchronizations().iterator(); it.hasNext();) { TransactionSynchronization synchronization = (TransactionSynchronization) it.next(); synchronization.beforeCommit(status.isReadOnly()); } } }
/** * JTA {@code beforeCompletion} callback: just invoked before commit. * <p>In case of an exception, the JTA transaction will be marked as rollback-only. * @see org.springframework.transaction.support.TransactionSynchronization#beforeCommit */ @Override public void beforeCompletion() { try { boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); this.springSynchronization.beforeCommit(readOnly); } catch (RuntimeException ex) { setRollbackOnlyIfPossible(); throw ex; } catch (Error err) { setRollbackOnlyIfPossible(); throw err; } finally { // Process Spring's beforeCompletion early, in order to avoid issues // with strict JTA implementations that issue warnings when doing JDBC // operations after transaction completion (e.g. Connection.getWarnings). this.beforeCompletionCalled = true; this.springSynchronization.beforeCompletion(); } }
/** * JTA {@code beforeCompletion} callback: just invoked before commit. * <p>In case of an exception, the JTA transaction will be marked as rollback-only. * @see org.springframework.transaction.support.TransactionSynchronization#beforeCommit */ @Override public void beforeCompletion() { try { boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); this.springSynchronization.beforeCommit(readOnly); } catch (RuntimeException ex) { setRollbackOnlyIfPossible(); throw ex; } catch (Error err) { setRollbackOnlyIfPossible(); throw err; } finally { // Process Spring's beforeCompletion early, in order to avoid issues // with strict JTA implementations that issue warnings when doing JDBC // operations after transaction completion (e.g. Connection.getWarnings). this.beforeCompletionCalled = true; this.springSynchronization.beforeCompletion(); } }