Search in sources :

Example 6 with TransactionCoordinator

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

the class InaccessibleJtaPlatformTests method testInaccessibleUserTransactionHandling.

@Test
public void testInaccessibleUserTransactionHandling() {
    // first, have JtaPlatform throw an exception
    try {
        final JtaPlatformInaccessibleImpl jtaPlatform = new JtaPlatformInaccessibleImpl(true);
        final TransactionCoordinator transactionCoordinator = new JtaTransactionCoordinatorImpl(transactionCoordinatorBuilder, owner, true, jtaPlatform, false, false);
        transactionCoordinator.getTransactionDriverControl().begin();
        fail("Expecting JtaPlatformInaccessibleException, but call succeeded");
    } catch (JtaPlatformInaccessibleException expected) {
    // expected condition
    } catch (Exception e) {
        fail("Expecting JtaPlatformInaccessibleException, but got " + e.getClass().getName());
    }
    // then, have it return null
    try {
        final JtaPlatformInaccessibleImpl jtaPlatform = new JtaPlatformInaccessibleImpl(false);
        final TransactionCoordinator transactionCoordinator = new JtaTransactionCoordinatorImpl(transactionCoordinatorBuilder, owner, true, jtaPlatform, false, false);
        transactionCoordinator.getTransactionDriverControl().begin();
        fail("Expecting JtaPlatformInaccessibleException, but call succeeded");
    } catch (JtaPlatformInaccessibleException expected) {
    // expected condition
    } catch (Exception e) {
        fail("Expecting JtaPlatformInaccessibleException, but got " + e.getClass().getName());
    }
}
Also used : JtaPlatformInaccessibleException(org.hibernate.resource.transaction.backend.jta.internal.JtaPlatformInaccessibleException) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) JtaPlatformInaccessibleException(org.hibernate.resource.transaction.backend.jta.internal.JtaPlatformInaccessibleException) Test(org.junit.Test)

Example 7 with TransactionCoordinator

use of org.hibernate.resource.transaction.spi.TransactionCoordinator 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)

Example 8 with TransactionCoordinator

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

the class InaccessibleJtaPlatformTests method testInaccessibleTransactionManagerHandling.

@Test
public void testInaccessibleTransactionManagerHandling() {
    // first, have JtaPlatform throw an exception
    try {
        final JtaPlatformInaccessibleImpl jtaPlatform = new JtaPlatformInaccessibleImpl(true);
        final TransactionCoordinator transactionCoordinator = new JtaTransactionCoordinatorImpl(transactionCoordinatorBuilder, owner, true, jtaPlatform, false, false);
        transactionCoordinator.getTransactionDriverControl().begin();
        fail("Expecting JtaPlatformInaccessibleException, but call succeeded");
    } catch (JtaPlatformInaccessibleException expected) {
    // expected condition
    } catch (Exception e) {
        fail("Expecting JtaPlatformInaccessibleException, but got " + e.getClass().getName());
    }
    // then, have it return null
    try {
        final JtaPlatformInaccessibleImpl jtaPlatform = new JtaPlatformInaccessibleImpl(false);
        final TransactionCoordinator transactionCoordinator = new JtaTransactionCoordinatorImpl(transactionCoordinatorBuilder, owner, true, jtaPlatform, false, false);
        transactionCoordinator.getTransactionDriverControl().begin();
        fail("Expecting JtaPlatformInaccessibleException, but call succeeded");
    } catch (JtaPlatformInaccessibleException expected) {
    // expected condition
    } catch (Exception e) {
        fail("Expecting JtaPlatformInaccessibleException, but got " + e.getClass().getName());
    }
}
Also used : JtaPlatformInaccessibleException(org.hibernate.resource.transaction.backend.jta.internal.JtaPlatformInaccessibleException) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) JtaPlatformInaccessibleException(org.hibernate.resource.transaction.backend.jta.internal.JtaPlatformInaccessibleException) Test(org.junit.Test)

Example 9 with TransactionCoordinator

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

the class TombstoneAccessDelegate method write.

protected void write(SharedSessionContractImplementor session, Object key, Object value) {
    TransactionCoordinator tc = session.getTransactionCoordinator();
    FutureUpdateSynchronization sync = new FutureUpdateSynchronization(tc, asyncWriteCache, requiresTransaction, key, value, region, session.getTimestamp());
    // The update will be invalidating all putFromLoads for the duration of expiration or until removed by the synchronization
    Tombstone tombstone = new Tombstone(sync.getUuid(), region.nextTimestamp() + region.getTombstoneExpiration());
    // The outcome of this operation is actually defined in TombstoneCallInterceptor
    // Metadata in PKVC are cleared and set in the interceptor, too
    writeCache.put(key, tombstone);
    tc.getLocalSynchronizations().registerSynchronization(sync);
}
Also used : Tombstone(org.hibernate.cache.infinispan.util.Tombstone) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator)

Example 10 with TransactionCoordinator

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

the class QueryResultsRegionImpl method put.

@Override
@SuppressWarnings("unchecked")
public void put(SharedSessionContractImplementor session, Object key, Object value) throws CacheException {
    if (checkValid()) {
        // See HHH-7898: Even with FAIL_SILENTLY flag, failure to write in transaction
        // fails the whole transaction. It is an Infinispan quirk that cannot be fixed
        // ISPN-5356 tracks that. This is because if the transaction continued the
        // value could be committed on backup owners, including the failed operation,
        // and the result would not be consistent.
        TransactionCoordinator tc = session.getTransactionCoordinator();
        if (tc != null && tc.isJoined()) {
            tc.getLocalSynchronizations().registerSynchronization(new PostTransactionQueryUpdate(tc, session, key, value));
            // no need to synchronize as the transaction will be accessed by only one thread
            Map map = transactionContext.get(session);
            if (map == null) {
                transactionContext.put(session, map = new HashMap());
            }
            map.put(key, value);
            return;
        }
        // Here we don't want to suspend the tx. If we do:
        // 1) We might be caching query results that reflect uncommitted
        // changes. No tx == no WL on cache node, so other threads
        // can prematurely see those query results
        // 2) No tx == immediate replication. More overhead, plus we
        // spread issue #1 above around the cluster
        // Add a zero (or quite low) timeout option so we don't block.
        // Ignore any TimeoutException. Basically we forego caching the
        // query result in order to avoid blocking.
        // Reads are done with suspended tx, so they should not hold the
        // lock for long.  Not caching the query result is OK, since
        // any subsequent read will just see the old result with its
        // out-of-date timestamp; that result will be discarded and the
        // db query performed again.
        putCache.put(key, value);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Aggregations

TransactionCoordinator (org.hibernate.resource.transaction.spi.TransactionCoordinator)10 Test (org.junit.Test)6 JdbcResourceLocalTransactionCoordinatorBuilderImpl (org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl)4 TransactionCoordinatorBuilder (org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder)3 TransactionException (org.hibernate.TransactionException)2 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)2 JtaPlatformInaccessibleException (org.hibernate.resource.transaction.backend.jta.internal.JtaPlatformInaccessibleException)2 JtaTransactionCoordinatorImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 Predicate (java.util.function.Predicate)1