Search in sources :

Example 1 with JtaTransactionObject

use of org.springframework.transaction.jta.JtaTransactionObject in project spring-framework by spring-projects.

the class DataSourceJtaTransactionTests method testJtaTransactionWithConnectionHolderStillBound.

@Test
public void testJtaTransactionWithConnectionHolderStillBound() throws Exception {
    @SuppressWarnings("serial") JtaTransactionManager ptm = new JtaTransactionManager(userTransaction) {

        @Override
        protected void doRegisterAfterCompletionWithJtaTransaction(JtaTransactionObject txObject, final List<TransactionSynchronization> synchronizations) throws RollbackException, SystemException {
            Thread async = new Thread() {

                @Override
                public void run() {
                    invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_COMMITTED);
                }
            };
            async.start();
            try {
                async.join();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    };
    TransactionTemplate tt = new TransactionTemplate(ptm);
    boolean condition2 = !TransactionSynchronizationManager.hasResource(dataSource);
    assertThat(condition2).as("Hasn't thread connection").isTrue();
    boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition1).as("JTA synchronizations not active").isTrue();
    given(userTransaction.getStatus()).willReturn(Status.STATUS_ACTIVE);
    for (int i = 0; i < 3; i++) {
        final boolean releaseCon = (i != 1);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("JTA synchronizations active").isTrue();
                boolean condition = !status.isNewTransaction();
                assertThat(condition).as("Is existing transaction").isTrue();
                Connection c = DataSourceUtils.getConnection(dataSource);
                assertThat(TransactionSynchronizationManager.hasResource(dataSource)).as("Has thread connection").isTrue();
                DataSourceUtils.releaseConnection(c, dataSource);
                c = DataSourceUtils.getConnection(dataSource);
                assertThat(TransactionSynchronizationManager.hasResource(dataSource)).as("Has thread connection").isTrue();
                if (releaseCon) {
                    DataSourceUtils.releaseConnection(c, dataSource);
                }
            }
        });
        if (!releaseCon) {
            assertThat(TransactionSynchronizationManager.hasResource(dataSource)).as("Still has connection holder").isTrue();
        } else {
            boolean condition = !TransactionSynchronizationManager.hasResource(dataSource);
            assertThat(condition).as("Hasn't thread connection").isTrue();
        }
        boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
        assertThat(condition).as("JTA synchronizations not active").isTrue();
    }
    verify(connection, times(3)).close();
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) JtaTransactionObject(org.springframework.transaction.jta.JtaTransactionObject) TransactionStatus(org.springframework.transaction.TransactionStatus) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) List(java.util.List) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Aggregations

Connection (java.sql.Connection)1 List (java.util.List)1 Test (org.junit.jupiter.api.Test)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)1 JtaTransactionObject (org.springframework.transaction.jta.JtaTransactionObject)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)1