Java 类org.springframework.transaction.support.TransactionSynchronization 实例源码

项目:lams    文件:SpringJtaSynchronizationAdapter.java   
/**
 * JTA {@code afterCompletion} callback: invoked after commit/rollback.
 * <p>Needs to invoke the Spring synchronization's {@code beforeCompletion}
 * at this late stage in case of a rollback, since there is no corresponding
 * callback with JTA.
 * @see org.springframework.transaction.support.TransactionSynchronization#beforeCompletion
 * @see org.springframework.transaction.support.TransactionSynchronization#afterCompletion
 */
@Override
public void afterCompletion(int status) {
    if (!this.beforeCompletionCalled) {
        // beforeCompletion not called before (probably because of JTA rollback).
        // Perform the cleanup here.
        this.springSynchronization.beforeCompletion();
    }
    // Call afterCompletion with the appropriate status indication.
    switch (status) {
        case Status.STATUS_COMMITTED:
            this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
            break;
        case Status.STATUS_ROLLEDBACK:
            this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
            break;
        default:
            this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
    }
}
项目:lams    文件:JtaAfterCompletionSynchronization.java   
@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);
    }
}
项目:spring4-understanding    文件:LobTypeTests.java   
@Test
public void testClobStringTypeWithFlushOnCommit() throws Exception {
    given(lobHandler.getClobAsString(rs, "column")).willReturn("content");

    ClobStringType type = new ClobStringType(lobHandler, null);
    assertEquals(1, type.sqlTypes().length);
    assertEquals(Types.CLOB, type.sqlTypes()[0]);
    assertEquals(String.class, type.returnedClass());
    assertTrue(type.equals("content", "content"));
    assertEquals("content", type.deepCopy("content"));
    assertFalse(type.isMutable());

    assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
    TransactionSynchronizationManager.initSynchronization();
    try {
        type.nullSafeSet(ps, "content", 1);
        List synchs = TransactionSynchronizationManager.getSynchronizations();
        assertEquals(1, synchs.size());
        ((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
    }
    finally {
        TransactionSynchronizationManager.clearSynchronization();
    }
    verify(lobCreator).setClobAsString(ps, 1, "content");
}
项目:spring4-understanding    文件:LobTypeTests.java   
@Test
public void testBlobStringTypeWithNull() throws Exception {
    given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(null);

    BlobStringType type = new BlobStringType(lobHandler, null);
    assertEquals(null, type.nullSafeGet(rs, new String[] {"column"}, null));
    TransactionSynchronizationManager.initSynchronization();
    try {
        type.nullSafeSet(ps, null, 1);
        List synchs = TransactionSynchronizationManager.getSynchronizations();
        assertEquals(1, synchs.size());
        ((TransactionSynchronization) synchs.get(0)).beforeCompletion();
    }
    finally {
        TransactionSynchronizationManager.clearSynchronization();
    }

    verify(lobCreator).setBlobAsBytes(ps, 1, null);
}
项目:spring4-understanding    文件:LobTypeTests.java   
@Test
public void testBlobSerializableTypeWithNull() throws Exception {
    given(lobHandler.getBlobAsBinaryStream(rs, "column")).willReturn(null);

    BlobSerializableType type = new BlobSerializableType(lobHandler, null);
    assertEquals(null, type.nullSafeGet(rs, new String[] {"column"}, null));
    TransactionSynchronizationManager.initSynchronization();
    try {
        type.nullSafeSet(ps, null, 1);
        List synchs = TransactionSynchronizationManager.getSynchronizations();
        assertEquals(1, synchs.size());
        ((TransactionSynchronization) synchs.get(0)).beforeCompletion();
    }
    finally {
        TransactionSynchronizationManager.clearSynchronization();
    }
    verify(lobCreator).setBlobAsBytes(ps, 1, null);
}
项目:spring4-understanding    文件:SpringJtaSynchronizationAdapter.java   
/**
 * JTA {@code afterCompletion} callback: invoked after commit/rollback.
 * <p>Needs to invoke the Spring synchronization's {@code beforeCompletion}
 * at this late stage in case of a rollback, since there is no corresponding
 * callback with JTA.
 * @see org.springframework.transaction.support.TransactionSynchronization#beforeCompletion
 * @see org.springframework.transaction.support.TransactionSynchronization#afterCompletion
 */
@Override
public void afterCompletion(int status) {
    if (!this.beforeCompletionCalled) {
        // beforeCompletion not called before (probably because of JTA rollback).
        // Perform the cleanup here.
        this.springSynchronization.beforeCompletion();
    }
    // Call afterCompletion with the appropriate status indication.
    switch (status) {
        case Status.STATUS_COMMITTED:
            this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
            break;
        case Status.STATUS_ROLLEDBACK:
            this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
            break;
        default:
            this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
    }
}
项目:spring4-understanding    文件:JtaAfterCompletionSynchronization.java   
@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);
    }
}
项目:spring4-understanding    文件:ApplicationListenerMethodTransactionalAdapter.java   
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        TransactionSynchronization transactionSynchronization = createTransactionSynchronization(event);
        TransactionSynchronizationManager.registerSynchronization(transactionSynchronization);
    }
    else if (this.annotation.fallbackExecution()) {
        if (this.annotation.phase() == TransactionPhase.AFTER_ROLLBACK) {
            logger.warn("Processing '" + event + "' as a fallback execution on AFTER_ROLLBACK phase.");
        }
        processEvent(event);
    }
    else {
        if (logger.isDebugEnabled()) {
            logger.debug("No transaction is running, skipping '" + event + "' for '" + this + "'");
        }
    }
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithExistingTransactionAndRollbackOnly() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@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);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithExistingTransactionAndRollbackOnlyAndNoGlobalRollback() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    ptm.setGlobalRollbackOnParticipationFailure(false);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithExistingTransactionAndSynchronizationOnActual() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(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) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithExistingAndPropagationSupports() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithPropagationSupports() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
项目:opencucina    文件:TransactionHandlerImpl.java   
private void handleStatus(int status, String type, Serializable... ids) {
    Message<?> callmess;

    if (TransactionSynchronization.STATUS_COMMITTED == status) {
        // TODO make CommitSuccess handle type/ids combo
        callmess = MessageBuilder.withPayload(new CommitSuccessEvent(ids))
                                 .setHeader(Operative.DESTINATION_NAME, "server.queue").build();
    } else {
        CompensateEvent compensateEvent = new CompensateEvent(status);

        compensateEvent.setIds(ids);
        compensateEvent.setType(type);
        callmess = MessageBuilder.withPayload(compensateEvent)
                                 .setHeader(Operative.DESTINATION_NAME, "server.queue").build();
        LOG.debug("Compensating for " + type + ":" + Arrays.toString(ids));
    }

    asyncChannel.send(callmess);
}
项目:spring-lock    文件:SessionTransactedLockSynchronizationManager.java   
private void releaseWithTransaction(final LockInstance lockInstance) {
   TransactionLockSynchronization synchronization = null;
   for (TransactionSynchronization transactionSynchronization : TransactionSynchronizationManager
            .getSynchronizations()) {
      if (transactionSynchronization instanceof TransactionLockSynchronization) {
         synchronization = (TransactionLockSynchronization) transactionSynchronization;
      }
   }

   if (null == synchronization) {
      synchronization = new TransactionLockSynchronization(this);
      TransactionSynchronizationManager.registerSynchronization(synchronization);
   }

   synchronization.add(lockInstance);
}
项目:artifactory    文件:SessionSynchronization.java   
@Override
public void afterCompletion(int status) {
    if (sessionActive) {
        boolean success = status == TransactionSynchronization.STATUS_COMMITTED;
        // Commit the locks/discard changes on rollback
        try {
            session.afterCompletion(success);
        } finally {
            session.releaseResources();
            StorageSessionHolder.removeSession();
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Session completed: '{}' in {}",
                sessionName, TimeUnitFormat.getTimeString(System.nanoTime() - startTime));
    }
}
项目:class-guard    文件:LobTypeTests.java   
@Test
public void testClobStringTypeWithFlushOnCommit() throws Exception {
    given(lobHandler.getClobAsString(rs, "column")).willReturn("content");

    ClobStringType type = new ClobStringType(lobHandler, null);
    assertEquals(1, type.sqlTypes().length);
    assertEquals(Types.CLOB, type.sqlTypes()[0]);
    assertEquals(String.class, type.returnedClass());
    assertTrue(type.equals("content", "content"));
    assertEquals("content", type.deepCopy("content"));
    assertFalse(type.isMutable());

    assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
    TransactionSynchronizationManager.initSynchronization();
    try {
        type.nullSafeSet(ps, "content", 1);
        List synchs = TransactionSynchronizationManager.getSynchronizations();
        assertEquals(1, synchs.size());
        ((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
    }
    finally {
        TransactionSynchronizationManager.clearSynchronization();
    }
    verify(lobCreator).setClobAsString(ps, 1, "content");
}
项目:class-guard    文件:LobTypeTests.java   
@Test
public void testBlobStringTypeWithNull() throws Exception {
    given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(null);

    BlobStringType type = new BlobStringType(lobHandler, null);
    assertEquals(null, type.nullSafeGet(rs, new String[] {"column"}, null));
    TransactionSynchronizationManager.initSynchronization();
    try {
        type.nullSafeSet(ps, null, 1);
        List synchs = TransactionSynchronizationManager.getSynchronizations();
        assertEquals(1, synchs.size());
        ((TransactionSynchronization) synchs.get(0)).beforeCompletion();
    }
    finally {
        TransactionSynchronizationManager.clearSynchronization();
    }

    verify(lobCreator).setBlobAsBytes(ps, 1, null);
}
项目:class-guard    文件:LobTypeTests.java   
@Test
public void testBlobSerializableTypeWithNull() throws Exception {
    given(lobHandler.getBlobAsBinaryStream(rs, "column")).willReturn(null);

    BlobSerializableType type = new BlobSerializableType(lobHandler, null);
    assertEquals(null, type.nullSafeGet(rs, new String[] {"column"}, null));
    TransactionSynchronizationManager.initSynchronization();
    try {
        type.nullSafeSet(ps, null, 1);
        List synchs = TransactionSynchronizationManager.getSynchronizations();
        assertEquals(1, synchs.size());
        ((TransactionSynchronization) synchs.get(0)).beforeCompletion();
    }
    finally {
        TransactionSynchronizationManager.clearSynchronization();
    }
    verify(lobCreator).setBlobAsBytes(ps, 1, null);
}
项目:class-guard    文件:LobTypeHandlerTests.java   
@Test
public void testClobStringTypeHandler() throws Exception {
    given(lobHandler.getClobAsString(rs, 1)).willReturn("content");

    ClobStringTypeHandler type = new ClobStringTypeHandler(lobHandler);
    assertEquals("content", type.valueOf("content"));
    assertEquals("content", type.getResult(rs, "column"));
    assertEquals("content", type.getResult(rs, 1));

    TransactionSynchronizationManager.initSynchronization();
    try {
        type.setParameter(ps, 1, "content", null);
        List synchs = TransactionSynchronizationManager.getSynchronizations();
        assertEquals(1, synchs.size());
        assertTrue(synchs.get(0).getClass().getName().endsWith("LobCreatorSynchronization"));
        ((TransactionSynchronization) synchs.get(0)).beforeCompletion();
        ((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
    }
    finally {
        TransactionSynchronizationManager.clearSynchronization();
    }
    verify(lobCreator).setClobAsString(ps, 1, "content");
}
项目:class-guard    文件:LobTypeHandlerTests.java   
@Test
public void testBlobByteArrayType() throws Exception {
    byte[] content = "content".getBytes();
    given(lobHandler.getBlobAsBytes(rs, 1)).willReturn(content);

    BlobByteArrayTypeHandler type = new BlobByteArrayTypeHandler(lobHandler);
    assertTrue(Arrays.equals(content, (byte[]) type.valueOf("content")));
    assertEquals(content, type.getResult(rs, "column"));
    assertEquals(content, type.getResult(rs, 1));

    TransactionSynchronizationManager.initSynchronization();
    try {
        type.setParameter(ps, 1, content, null);
        List synchs = TransactionSynchronizationManager.getSynchronizations();
        assertEquals(1, synchs.size());
        ((TransactionSynchronization) synchs.get(0)).beforeCompletion();
        ((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
    }
    finally {
        TransactionSynchronizationManager.clearSynchronization();
    }
    verify(lobCreator).setBlobAsBytes(ps, 1, content);
}
项目:class-guard    文件:LobTypeHandlerTests.java   
@Test
public void testBlobSerializableTypeWithNull() throws Exception {
    given(lobHandler.getBlobAsBinaryStream(rs, 1)).willReturn(null);

    BlobSerializableTypeHandler type = new BlobSerializableTypeHandler(lobHandler);
    assertEquals(null, type.valueOf(null));
    assertEquals(null, type.getResult(rs, "column"));
    assertEquals(null, type.getResult(rs, 1));

    TransactionSynchronizationManager.initSynchronization();
    try {
        type.setParameter(ps, 1, null, null);
        List synchs = TransactionSynchronizationManager.getSynchronizations();
        assertEquals(1, synchs.size());
        ((TransactionSynchronization) synchs.get(0)).beforeCompletion();
    }
    finally {
        TransactionSynchronizationManager.clearSynchronization();
    }
    verify(lobCreator).setBlobAsBytes(ps, 1, null);
}
项目:class-guard    文件:SpringJtaSynchronizationAdapter.java   
/**
 * JTA {@code afterCompletion} callback: invoked after commit/rollback.
 * <p>Needs to invoke the Spring synchronization's {@code beforeCompletion}
 * at this late stage in case of a rollback, since there is no corresponding
 * callback with JTA.
 * @see org.springframework.transaction.support.TransactionSynchronization#beforeCompletion
 * @see org.springframework.transaction.support.TransactionSynchronization#afterCompletion
 */
public void afterCompletion(int status) {
    if (!this.beforeCompletionCalled) {
        // beforeCompletion not called before (probably because of JTA rollback).
        // Perform the cleanup here.
        this.springSynchronization.beforeCompletion();
    }
    // Call afterCompletion with the appropriate status indication.
    switch (status) {
        case Status.STATUS_COMMITTED:
            this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
            break;
        case Status.STATUS_ROLLEDBACK:
            this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
            break;
        default:
            this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
    }
}
项目:class-guard    文件:JtaAfterCompletionSynchronization.java   
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);
    }
}
项目:class-guard    文件:JtaTransactionManagerTests.java   
public void testJtaTransactionManagerWithRollbackAndSynchronizationOnActual() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, 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);
    tt.setTimeout(10);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setTransactionTimeout(10);
    verify(ut).begin();
    verify(ut).rollback();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
项目:class-guard    文件:JtaTransactionManagerTests.java   
public void testJtaTransactionManagerWithExistingTransactionAndRollbackOnly() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:class-guard    文件:JtaTransactionManagerTests.java   
public void testJtaTransactionManagerWithExistingTransactionAndRollbackOnlyAndNoGlobalRollback() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    ptm.setGlobalRollbackOnParticipationFailure(false);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:class-guard    文件:JtaTransactionManagerTests.java   
public void testJtaTransactionManagerWithExistingTransactionAndSynchronizationOnActual() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(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) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:class-guard    文件:JtaTransactionManagerTests.java   
public void testJtaTransactionManagerWithExistingAndPropagationSupports() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:class-guard    文件:JtaTransactionManagerTests.java   
public void testJtaTransactionManagerWithPropagationSupports() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
项目:gocd    文件:PipelineTimelineTest.java   
@Test
public void shouldRemove_NewlyAddedTimelineEntries_fromAllCollections_UponRollback() throws Exception {
    Collection<PipelineTimelineEntry> allEntries;

    stubTransactionSynchronization();
    setupTransactionTemplateStub(TransactionSynchronization.STATUS_COMMITTED, true);
    final PipelineTimeline timeline = new PipelineTimeline(pipelineRepository, transactionTemplate, transactionSynchronizationManager);

    stubPipelineRepository(timeline, true, first, second);
    timeline.update();
    allEntries = timeline.getEntriesFor("pipeline");

    setupTransactionTemplateStub(TransactionSynchronization.STATUS_ROLLED_BACK, false);

    stubPipelineRepository(timeline, false, third, fourth);
    timeline.update();
    allEntries = timeline.getEntriesFor("pipeline");

    assertThat(timeline.maximumId(), is(2L));
    assertThat(timeline.getEntriesFor("pipeline").size(), is(2));
    assertThat(allEntries, hasItems(first, second));
    assertThat(timeline.instanceCount(new CaseInsensitiveString("pipeline")), is(2));
    assertThat(timeline.instanceFor(new CaseInsensitiveString("pipeline"), 0), is(first));
    assertThat(timeline.instanceFor(new CaseInsensitiveString("pipeline"), 1), is(second));
}
项目:gocd    文件:PipelineTimelineTest.java   
private void setupTransactionTemplateStub(final int status, final boolean restub) throws Exception {
    this.txnStatus = status;
    if (restub) {
        when(transactionTemplate.execute(Mockito.any(TransactionCallback.class))).thenAnswer(new Answer<Object>() {
            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                TransactionCallback callback = (TransactionCallback) invocationOnMock.getArguments()[0];
                callback.doInTransaction(null);
                if (txnStatus == TransactionSynchronization.STATUS_COMMITTED) {
                    transactionSynchronization.afterCommit();
                }
                transactionSynchronization.afterCompletion(txnStatus);
                return null;
            }
        });
    }
}
项目:gocd    文件:TransactionSynchronizationManagerTest.java   
@Test
public void shouldRegisterSynchronization_andNotCallItOnTransactionFailure() {
    final TransactionSynchronizationManager synchronizationManager = new TransactionSynchronizationManager();

    final TransactionSynchronization synchronization = mock(TransactionSynchronization.class);
    try {
        transactionTemplate.execute(new org.springframework.transaction.support.TransactionCallbackWithoutResult() {
            @Override protected void doInTransactionWithoutResult(TransactionStatus status) {
                synchronizationManager.registerSynchronization(synchronization);
                throw new RuntimeException();
            }
        });
    } catch (Exception e) {
        //ignore
    }
    verify(synchronization, never()).afterCommit();
}
项目:gocd    文件:TransactionContext.java   
public void transactionPushed() {
    if (! isTransactionBodyExecuting()) {
        if (isInTransactionSurrounding() && txnFinished) {
            throw new RuntimeException(TOO_MANY_TXNS_IN_SURROUNDING);
        }
        txnFinished = false;
    }

    ensureCanPush(txnActive);
    txnActive--;

    if (! futureSynchronizations.isEmpty()) {
        for (TransactionSynchronization futureSynchronization : futureSynchronizations) {
            doRegisterSynchronization(futureSynchronization);
        }
        clearFutureSynchronizations();
    }
}
项目:camunda-bpm-platform    文件:SpringTransactionContext.java   
public SpringTransactionContext(PlatformTransactionManager transactionManager, CommandContext commandContext) {
  this.transactionManager = transactionManager;
  this.commandContext = commandContext;
  TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
    @Override
    public void beforeCommit(boolean readOnly) {
      lastTransactionState = TransactionState.COMMITTING;
    }
    @Override
    public void afterCommit() {
      lastTransactionState = TransactionState.COMMITTED;
    }
    @Override
    public void beforeCompletion() {
      lastTransactionState = TransactionState.ROLLINGBACK;
    }
    @Override
    public void afterCompletion(int status) {
      if(TransactionSynchronization.STATUS_ROLLED_BACK == status) {
        lastTransactionState = TransactionState.ROLLED_BACK;
      }
    }
  });
}
项目:tipi-engine    文件:TopProcessGroupLauncher.java   
@Override
public void afterCompletion(int status) {
    if (TransactionSynchronization.STATUS_COMMITTED == status) {
        startNewThreadDeffered(runner);
    } else if (TransactionSynchronization.STATUS_ROLLED_BACK == status) {
        removeRunning(runner.getActivityId());
    } else {
        Assert.fail("Impossible.");
    }
}
项目:org.ops4j.pax.transx    文件:GeronimoPlatformTransactionManager.java   
public SuspendedResourcesHolder(
        Object suspendedResources, List<TransactionSynchronization> suspendedSynchronizations, String name, boolean readOnly) {

    this.suspendedResources = suspendedResources;
    this.suspendedSynchronizations = suspendedSynchronizations;
    this.name = name;
    this.readOnly = readOnly;
}
项目:lams    文件:JtaTransactionManager.java   
/**
 * Register a JTA synchronization on the JTA TransactionManager, for calling
 * {@code afterCompletion} on the given Spring TransactionSynchronizations.
 * <p>The default implementation registers the synchronizations on the
 * JTA 1.1 TransactionSynchronizationRegistry, if available, or on the
 * JTA TransactionManager's current Transaction - again, if available.
 * If none of the two is available, a warning will be logged.
 * <p>Can be overridden in subclasses, for specific JTA implementations.
 * @param txObject the current transaction object
 * @param synchronizations List of TransactionSynchronization objects
 * @throws RollbackException if thrown by JTA methods
 * @throws SystemException if thrown by JTA methods
 * @see #getTransactionManager()
 * @see javax.transaction.Transaction#registerSynchronization
 * @see javax.transaction.TransactionSynchronizationRegistry#registerInterposedSynchronization
 */
protected void doRegisterAfterCompletionWithJtaTransaction(
        JtaTransactionObject txObject, List<TransactionSynchronization> synchronizations)
        throws RollbackException, SystemException {

    int jtaStatus = txObject.getUserTransaction().getStatus();
    if (jtaStatus == Status.STATUS_NO_TRANSACTION) {
        throw new RollbackException("JTA transaction already completed - probably rolled back");
    }
    if (jtaStatus == Status.STATUS_ROLLEDBACK) {
        throw new RollbackException("JTA transaction already rolled back (probably due to a timeout)");
    }

    if (this.transactionSynchronizationRegistry != null) {
        // JTA 1.1 TransactionSynchronizationRegistry available - use it.
        this.transactionSynchronizationRegistry.registerInterposedSynchronization(
                new JtaAfterCompletionSynchronization(synchronizations));
    }

    else if (getTransactionManager() != null) {
        // At least the JTA TransactionManager available - use that one.
        Transaction transaction = getTransactionManager().getTransaction();
        if (transaction == null) {
            throw new IllegalStateException("No JTA Transaction available");
        }
        transaction.registerSynchronization(new JtaAfterCompletionSynchronization(synchronizations));
    }

    else {
        // No JTA TransactionManager available - log a warning.
        logger.warn("Participating in existing JTA transaction, but no JTA TransactionManager available: " +
                "cannot register Spring after-completion callbacks with outer JTA transaction - " +
                "processing Spring after-completion callbacks with outcome status 'unknown'");
        invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_UNKNOWN);
    }
}
项目:lams    文件:WebSphereUowTransactionManager.java   
/**
 * Registers the synchronizations as interposed JTA Synchronization on the UOWManager.
 */
@Override
protected void doRegisterAfterCompletionWithJtaTransaction(
        JtaTransactionObject txObject, List<TransactionSynchronization> synchronizations) {

    this.uowManager.registerInterposedSynchronization(new JtaAfterCompletionSynchronization(synchronizations));
}