Search in sources :

Example 1 with JtaTransactionObject

use of cn.taketoday.transaction.jta.JtaTransactionObject in project today-framework by TAKETODAY.

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(cn.taketoday.transaction.support.TransactionTemplate) Connection(java.sql.Connection) JtaTransactionObject(cn.taketoday.transaction.jta.JtaTransactionObject) TransactionStatus(cn.taketoday.transaction.TransactionStatus) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) List(java.util.List) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 2 with JtaTransactionObject

use of cn.taketoday.transaction.jta.JtaTransactionObject in project today-infrastructure by TAKETODAY.

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(cn.taketoday.transaction.support.TransactionTemplate) Connection(java.sql.Connection) JtaTransactionObject(cn.taketoday.transaction.jta.JtaTransactionObject) TransactionStatus(cn.taketoday.transaction.TransactionStatus) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) List(java.util.List) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Aggregations

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