Search in sources :

Example 31 with TransactionSynchronizationAdapter

use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.

the class TransactionTemplateTest method shouldAllowRegistrationOfTransactionSynchronization_inTransactionSurroundingBlock_andNotExecuteSynchronizationIfTransactionNeverHappens.

@Test
public void shouldAllowRegistrationOfTransactionSynchronization_inTransactionSurroundingBlock_andNotExecuteSynchronizationIfTransactionNeverHappens() {
    TransactionTemplate template = new TransactionTemplate(transactionTemplate);
    final boolean[] afterCommitHappened = new boolean[1];
    String returnVal = (String) template.transactionSurrounding(new TransactionTemplate.TransactionSurrounding<RuntimeException>() {

        public Object surrounding() {
            transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCommit() {
                    afterCommitHappened[0] = true;
                }
            });
            return "bar";
        }
    });
    assertThat(returnVal, is("bar"));
    assertThat(afterCommitHappened[0], is(false));
}
Also used : TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Example 32 with TransactionSynchronizationAdapter

use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.

the class TransactionTemplateTest method shouldAllowRegistrationOfTransactionSynchronization_inTransactionSurroundingBlock_andExecuteAppropriateHooks.

@Test
public void shouldAllowRegistrationOfTransactionSynchronization_inTransactionSurroundingBlock_andExecuteAppropriateHooks() {
    final TransactionTemplate template = new TransactionTemplate(transactionTemplate);
    final boolean[] afterCommitHappened = new boolean[1];
    final boolean[] transactionWasActiveInSurrounding = new boolean[1];
    final boolean[] transactionWasActiveInTransaction = new boolean[1];
    String returnVal = (String) template.transactionSurrounding(new TransactionTemplate.TransactionSurrounding<RuntimeException>() {

        public Object surrounding() {
            transactionWasActiveInSurrounding[0] = transactionSynchronizationManager.isTransactionBodyExecuting();
            transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCommit() {
                    afterCommitHappened[0] = true;
                }
            });
            return template.execute(new org.springframework.transaction.support.TransactionCallback() {

                public Object doInTransaction(TransactionStatus status) {
                    transactionWasActiveInTransaction[0] = transactionSynchronizationManager.isTransactionBodyExecuting();
                    return "foo";
                }
            });
        }
    });
    assertThat(returnVal, is("foo"));
    assertThat(afterCommitHappened[0], is(true));
    assertThat(transactionWasActiveInSurrounding[0], is(false));
    assertThat(transactionWasActiveInTransaction[0], is(true));
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Example 33 with TransactionSynchronizationAdapter

use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.

the class TransactionTemplateTest method shouldPropagateExceptionsOutOfTransactionSurrounding.

@Test
public void shouldPropagateExceptionsOutOfTransactionSurrounding() throws IOException {
    TransactionTemplate template = new TransactionTemplate(transactionTemplate);
    final boolean[] afterCommitHappened = new boolean[1];
    String returnVal = null;
    try {
        returnVal = (String) template.transactionSurrounding(new TransactionTemplate.TransactionSurrounding<IOException>() {

            public Object surrounding() throws IOException {
                transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                    @Override
                    public void afterCommit() {
                        afterCommitHappened[0] = true;
                    }
                });
                throw new IOException("boo ha!");
            }
        });
        fail("should have propagated exception");
    } catch (IOException e) {
        assertThat(e.getMessage(), is("boo ha!"));
    }
    assertThat(returnVal, nullValue());
    assertThat(afterCommitHappened[0], is(false));
}
Also used : IOException(java.io.IOException) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Example 34 with TransactionSynchronizationAdapter

use of org.springframework.transaction.support.TransactionSynchronizationAdapter in project gocd by gocd.

the class GoCacheTest method shouldStartServingThingsOutOfCacheOnceTransactionCompletes.

@Test
public void shouldStartServingThingsOutOfCacheOnceTransactionCompletes() {
    final SqlMapClientDaoSupport daoSupport = new SqlMapClientDaoSupport(goCache, sqlMapClient, systemEnvironment, databaseStrategy);
    goCache.put("foo", "bar");
    final String[] valueInCleanTxn = new String[1];
    final String[] valueInDirtyTxn = new String[1];
    final String[] valueInAfterCommit = new String[1];
    final String[] valueInAfterCompletion = new String[1];
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            valueInCleanTxn[0] = (String) goCache.get("foo");
            User user = new User("loser", "Massive Loser", "boozer@loser.com");
            userSqlMapDao.saveOrUpdate(user);
            valueInDirtyTxn[0] = (String) goCache.get("foo");
            transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                @Override
                public void afterCommit() {
                    valueInAfterCommit[0] = (String) goCache.get("foo");
                }

                @Override
                public void afterCompletion(int status) {
                    valueInAfterCompletion[0] = (String) goCache.get("foo");
                }
            });
        }
    });
    assertThat(valueInCleanTxn[0], is("bar"));
    assertThat(valueInDirtyTxn[0], is(nullValue()));
    assertThat(valueInAfterCommit[0], is("bar"));
    assertThat(valueInAfterCompletion[0], is("bar"));
}
Also used : User(com.thoughtworks.go.domain.User) NullUser(com.thoughtworks.go.domain.NullUser) TransactionStatus(org.springframework.transaction.TransactionStatus) Matchers.containsString(org.hamcrest.Matchers.containsString) SqlMapClientDaoSupport(com.thoughtworks.go.server.transaction.SqlMapClientDaoSupport) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Aggregations

TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)34 Test (org.junit.Test)16 TransactionStatus (org.springframework.transaction.TransactionStatus)15 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)14 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 UserTransaction (javax.transaction.UserTransaction)7 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)7 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)6 TransactionCallback (org.springframework.transaction.support.TransactionCallback)5 Pipeline (com.thoughtworks.go.domain.Pipeline)3 SystemException (javax.transaction.SystemException)3 PipelineState (com.thoughtworks.go.domain.PipelineState)2 GitCommand (com.thoughtworks.go.domain.materials.git.GitCommand)2 TransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TransactionSynchronizationManager)2 File (java.io.File)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 SimpleTransactionStatus (org.springframework.transaction.support.SimpleTransactionStatus)2 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)2