Search in sources :

Example 1 with TransactionCoordinatorBuilder

use of org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder in project hibernate-orm by hibernate.

the class BasicJdbcTransactionTests method basicUsageTest.

@Test
public void basicUsageTest() throws Exception {
    final TransactionCoordinatorOwnerTestingImpl owner = new TransactionCoordinatorOwnerTestingImpl();
    final JdbcResourceLocalTransactionCoordinatorBuilderImpl transactionCoordinatorBuilder = new JdbcResourceLocalTransactionCoordinatorBuilderImpl();
    final TransactionCoordinator transactionCoordinator = transactionCoordinatorBuilder.buildTransactionCoordinator(owner, new TransactionCoordinatorBuilder.Options() {

        @Override
        public boolean shouldAutoJoinTransaction() {
            return false;
        }
    });
    SynchronizationCollectorImpl sync = new SynchronizationCollectorImpl();
    transactionCoordinator.getLocalSynchronizations().registerSynchronization(sync);
    transactionCoordinator.getTransactionDriverControl().begin();
    assertEquals(0, sync.getBeforeCompletionCount());
    assertEquals(0, sync.getSuccessfulCompletionCount());
    assertEquals(0, sync.getFailedCompletionCount());
    transactionCoordinator.getTransactionDriverControl().commit();
    assertEquals(1, sync.getBeforeCompletionCount());
    assertEquals(1, sync.getSuccessfulCompletionCount());
    assertEquals(0, sync.getFailedCompletionCount());
}
Also used : SynchronizationCollectorImpl(org.hibernate.test.resource.common.SynchronizationCollectorImpl) JdbcResourceLocalTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) TransactionCoordinatorBuilder(org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder) Test(org.junit.Test)

Example 2 with TransactionCoordinatorBuilder

use of org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder in project hibernate-orm by hibernate.

the class BasicJdbcTransactionTests method testSynchronizationFailure.

@Test
@SuppressWarnings("EmptyCatchBlock")
public void testSynchronizationFailure() {
    final TransactionCoordinatorOwnerTestingImpl owner = new TransactionCoordinatorOwnerTestingImpl();
    final JdbcResourceLocalTransactionCoordinatorBuilderImpl transactionCoordinatorBuilder = new JdbcResourceLocalTransactionCoordinatorBuilderImpl();
    final TransactionCoordinator transactionCoordinator = transactionCoordinatorBuilder.buildTransactionCoordinator(owner, new TransactionCoordinatorBuilder.Options() {

        @Override
        public boolean shouldAutoJoinTransaction() {
            return false;
        }
    });
    assertEquals(TransactionStatus.NOT_ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    transactionCoordinator.getLocalSynchronizations().registerSynchronization(SynchronizationErrorImpl.forBefore());
    transactionCoordinator.getTransactionDriverControl().begin();
    assertEquals(TransactionStatus.ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    try {
        transactionCoordinator.getTransactionDriverControl().commit();
    } catch (Exception expected) {
    } finally {
        assertEquals(TransactionStatus.NOT_ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    }
}
Also used : JdbcResourceLocalTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) TransactionCoordinatorBuilder(org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder) TransactionException(org.hibernate.TransactionException) Test(org.junit.Test)

Example 3 with TransactionCoordinatorBuilder

use of org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder in project hibernate-orm by hibernate.

the class LegacySettingInitiatorTest method testLegacySettingSelection.

@Test
public void testLegacySettingSelection() {
    final TransactionCoordinatorBuilderInitiator initiator = new TransactionCoordinatorBuilderInitiator();
    TransactionCoordinatorBuilder builder = initiator.initiateService(Collections.singletonMap(TransactionCoordinatorBuilderInitiator.LEGACY_SETTING_NAME, "org.hibernate.transaction.JDBCTransactionFactory"), bsr);
    assertThat(builder, instanceOf(JdbcResourceLocalTransactionCoordinatorBuilderImpl.class));
    builder = initiator.initiateService(Collections.singletonMap(TransactionCoordinatorBuilderInitiator.LEGACY_SETTING_NAME, "org.hibernate.transaction.JTATransactionFactory"), bsr);
    assertThat(builder, instanceOf(JtaTransactionCoordinatorBuilderImpl.class));
    builder = initiator.initiateService(Collections.singletonMap(TransactionCoordinatorBuilderInitiator.LEGACY_SETTING_NAME, "org.hibernate.transaction.CMTTransactionFactory"), bsr);
    assertThat(builder, instanceOf(JtaTransactionCoordinatorBuilderImpl.class));
}
Also used : JtaTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl) TransactionCoordinatorBuilderInitiator(org.hibernate.resource.transaction.internal.TransactionCoordinatorBuilderInitiator) JdbcResourceLocalTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl) TransactionCoordinatorBuilder(org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder) Test(org.junit.Test)

Example 4 with TransactionCoordinatorBuilder

use of org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder in project hibernate-orm by hibernate.

the class BasicJdbcTransactionTests method testMarkRollbackOnly.

@Test
@SuppressWarnings("EmptyCatchBlock")
public void testMarkRollbackOnly() {
    final TransactionCoordinatorOwnerTestingImpl owner = new TransactionCoordinatorOwnerTestingImpl();
    final JdbcResourceLocalTransactionCoordinatorBuilderImpl transactionCoordinatorBuilder = new JdbcResourceLocalTransactionCoordinatorBuilderImpl();
    final TransactionCoordinator transactionCoordinator = transactionCoordinatorBuilder.buildTransactionCoordinator(owner, new TransactionCoordinatorBuilder.Options() {

        @Override
        public boolean shouldAutoJoinTransaction() {
            return false;
        }
    });
    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) JdbcResourceLocalTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) TransactionCoordinatorBuilder(org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder) Test(org.junit.Test)

Aggregations

JdbcResourceLocalTransactionCoordinatorBuilderImpl (org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl)4 TransactionCoordinatorBuilder (org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder)4 Test (org.junit.Test)4 TransactionCoordinator (org.hibernate.resource.transaction.spi.TransactionCoordinator)3 TransactionException (org.hibernate.TransactionException)2 JtaTransactionCoordinatorBuilderImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl)1 TransactionCoordinatorBuilderInitiator (org.hibernate.resource.transaction.internal.TransactionCoordinatorBuilderInitiator)1 SynchronizationCollectorImpl (org.hibernate.test.resource.common.SynchronizationCollectorImpl)1