Example usage for org.springframework.transaction.jta JtaTransactionManager JtaTransactionManager

List of usage examples for org.springframework.transaction.jta JtaTransactionManager JtaTransactionManager

Introduction

In this page you can find the example usage for org.springframework.transaction.jta JtaTransactionManager JtaTransactionManager.

Prototype

public JtaTransactionManager(UserTransaction userTransaction, TransactionManager transactionManager) 

Source Link

Document

Create a new JtaTransactionManager instance.

Usage

From source file:com._4dconcept.springframework.data.marklogic.datasource.DatabaseConnectorJtaTransactionTest.java

private void doTestJtaTransactionWithPropagationRequiresNewAndBeginException(boolean suspendException,
        final boolean openOuterConnection, final boolean useTransactionAwareContentSource) throws Exception {

    given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE,
            Status.STATUS_ACTIVE);
    if (suspendException) {
        given(transactionManager.suspend()).willThrow(new SystemException());
    } else {/*from   w  w  w .j  av a  2 s .  c om*/
        given(transactionManager.suspend()).willReturn(transaction);
        willThrow(new SystemException()).given(userTransaction).begin();
    }

    given(session.getTransactionMode()).willReturn(Session.TransactionMode.QUERY);

    final ContentSource dsToUse = useTransactionAwareContentSource
            ? new TransactionAwareContentSourceProxy(contentSource)
            : contentSource;
    if (dsToUse instanceof TransactionAwareContentSourceProxy) {
        ((TransactionAwareContentSourceProxy) dsToUse).setReobtainTransactionalSessions(true);
    }

    JtaTransactionManager ptm = new JtaTransactionManager(userTransaction, transactionManager);
    final TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(dsToUse));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

    try {
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(dsToUse));
                assertTrue("JTA synchronizations active",
                        TransactionSynchronizationManager.isSynchronizationActive());
                assertTrue("Is new transaction", status.isNewTransaction());

                Session c = ContentSourceUtils.getSession(dsToUse);
                assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse));
                c.getTransactionMode();
                ContentSourceUtils.releaseSession(c, dsToUse);

                c = ContentSourceUtils.getSession(dsToUse);
                assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(dsToUse));
                if (!openOuterConnection) {
                    ContentSourceUtils.releaseSession(c, dsToUse);
                }

                try {
                    tt.execute(new TransactionCallbackWithoutResult() {
                        @Override
                        protected void doInTransactionWithoutResult(TransactionStatus status)
                                throws RuntimeException {
                            assertTrue("Hasn't thread session",
                                    !TransactionSynchronizationManager.hasResource(dsToUse));
                            assertTrue("JTA synchronizations active",
                                    TransactionSynchronizationManager.isSynchronizationActive());
                            assertTrue("Is new transaction", status.isNewTransaction());

                            Session c = ContentSourceUtils.getSession(dsToUse);
                            assertTrue("Has thread session",
                                    TransactionSynchronizationManager.hasResource(dsToUse));
                            ContentSourceUtils.releaseSession(c, dsToUse);

                            c = ContentSourceUtils.getSession(dsToUse);
                            assertTrue("Has thread session",
                                    TransactionSynchronizationManager.hasResource(dsToUse));
                            ContentSourceUtils.releaseSession(c, dsToUse);
                        }
                    });
                } finally {
                    if (openOuterConnection) {
                        c.getTransactionMode();
                        ContentSourceUtils.releaseSession(c, dsToUse);

                    }
                }
            }
        });

        fail("Should have thrown TransactionException");
    } catch (TransactionException ex) {
        // expected
    }

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(dsToUse));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

    verify(userTransaction).begin();
    if (suspendException) {
        verify(userTransaction).rollback();
    }

    if (suspendException) {
        verify(session, atLeastOnce()).close();
    } else {
        verify(session, never()).close();
    }
}