Search in sources :

Example 21 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class BitronixJtaPlatform method locateTransactionManager.

@Override
protected TransactionManager locateTransactionManager() {
    try {
        Class transactionManagerServicesClass = serviceRegistry().getService(ClassLoaderService.class).classForName(TM_CLASS_NAME);
        final Method getTransactionManagerMethod = transactionManagerServicesClass.getMethod("getTransactionManager");
        return (TransactionManager) getTransactionManagerMethod.invoke(null);
    } catch (Exception e) {
        throw new JtaPlatformException("Could not locate Bitronix TransactionManager", e);
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) Method(java.lang.reflect.Method) JtaPlatformException(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException) JtaPlatformException(org.hibernate.engine.transaction.jta.platform.spi.JtaPlatformException) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 22 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method explicitJoinOutsideTxnTest.

@Test
@SuppressWarnings("EmptyCatchBlock")
public void explicitJoinOutsideTxnTest() throws Exception {
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    final JtaTransactionCoordinatorImpl transactionCoordinator = buildTransactionCoordinator(false);
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    // try to force a join, should fail...
    try {
        transactionCoordinator.explicitJoin();
        fail("Expecting explicitJoin() call outside of transaction to fail");
    } catch (TransactionRequiredForJoinException expected) {
    }
}
Also used : TransactionRequiredForJoinException(org.hibernate.resource.transaction.TransactionRequiredForJoinException) TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 23 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method rollbackBmtUsageTest.

@Test
public void rollbackBmtUsageTest() throws Exception {
    final JtaTransactionCoordinatorImpl transactionCoordinator = buildTransactionCoordinator(true);
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    // begin the transaction
    transactionCoordinator.getTransactionDriverControl().begin();
    assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
    assertTrue(transactionCoordinator.isSynchronizationRegistered());
    // create and add a local Synchronization
    SynchronizationCollectorImpl localSync = new SynchronizationCollectorImpl();
    transactionCoordinator.getLocalSynchronizations().registerSynchronization(localSync);
    // rollback the transaction
    transactionCoordinator.getTransactionDriverControl().rollback();
    // post conditions
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertEquals(0, localSync.getBeforeCompletionCount());
    assertEquals(0, localSync.getSuccessfulCompletionCount());
    assertEquals(1, localSync.getFailedCompletionCount());
}
Also used : SynchronizationCollectorImpl(org.hibernate.test.resource.common.SynchronizationCollectorImpl) TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 24 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method assureMultipleJoinCallsNoOp.

@Test
public void assureMultipleJoinCallsNoOp() throws Exception {
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    // begin the transaction
    tm.begin();
    assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
    final JtaTransactionCoordinatorImpl transactionCoordinator = buildTransactionCoordinator(false);
    // no auto-join now
    assertFalse(transactionCoordinator.isJoined());
    transactionCoordinator.explicitJoin();
    assertTrue(transactionCoordinator.isJoined());
    transactionCoordinator.explicitJoin();
    transactionCoordinator.explicitJoin();
    transactionCoordinator.explicitJoin();
    transactionCoordinator.explicitJoin();
    assertTrue(transactionCoordinator.isJoined());
    // create and add a local Synchronization
    SynchronizationCollectorImpl localSync = new SynchronizationCollectorImpl();
    transactionCoordinator.getLocalSynchronizations().registerSynchronization(localSync);
    // commit the transaction
    tm.commit();
    // post conditions
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertFalse(transactionCoordinator.isSynchronizationRegistered());
    assertEquals(1, localSync.getBeforeCompletionCount());
    assertEquals(1, localSync.getSuccessfulCompletionCount());
    assertEquals(0, localSync.getFailedCompletionCount());
}
Also used : SynchronizationCollectorImpl(org.hibernate.test.resource.common.SynchronizationCollectorImpl) TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Example 25 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method testMarkRollbackOnly.

@Test
@SuppressWarnings("EmptyCatchBlock")
public void testMarkRollbackOnly() throws Exception {
    JtaTransactionCoordinatorImpl transactionCoordinator = new JtaTransactionCoordinatorImpl(transactionCoordinatorBuilder, owner, true, JtaPlatformStandardTestingImpl.INSTANCE, preferUserTransactions(), true);
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertEquals(TransactionStatus.NOT_ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    transactionCoordinator.getTransactionDriverControl().begin();
    assertEquals(TransactionStatus.ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    transactionCoordinator.getTransactionDriverControl().markRollbackOnly();
    assertEquals(TransactionStatus.MARKED_ROLLBACK, transactionCoordinator.getTransactionDriverControl().getStatus());
    try {
        transactionCoordinator.getTransactionDriverControl().commit();
    } catch (TransactionException expected) {
    } finally {
        assertEquals(TransactionStatus.NOT_ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    }
}
Also used : TransactionException(org.hibernate.TransactionException) TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) Test(org.junit.Test)

Aggregations

TransactionManager (javax.transaction.TransactionManager)110 Test (org.junit.Test)40 Transaction (javax.transaction.Transaction)24 SystemException (javax.transaction.SystemException)22 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)15 UserTransaction (javax.transaction.UserTransaction)14 JtaTransactionCoordinatorImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)12 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)11 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)9 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)9 Method (java.lang.reflect.Method)7 EntityManager (javax.persistence.EntityManager)7 NotSupportedException (javax.transaction.NotSupportedException)7 RollbackException (javax.transaction.RollbackException)7 SynchronizationCollectorImpl (org.hibernate.test.resource.common.SynchronizationCollectorImpl)6 TestForIssue (org.hibernate.testing.TestForIssue)6 IOException (java.io.IOException)5 InitialContext (javax.naming.InitialContext)5 DataSource (javax.sql.DataSource)5 JtaPlatform (org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform)5