Search in sources :

Example 1 with TransactionCoordinator

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

the class AbstractRegionAccessStrategyTest method mockedSession.

protected SharedSessionContractImplementor mockedSession() {
    SessionMock session = mock(SessionMock.class);
    when(session.isClosed()).thenReturn(false);
    when(session.getTimestamp()).thenReturn(TIME_SERVICE.wallClockTime());
    if (jtaPlatform == BatchModeJtaPlatform.class) {
        BatchModeTransactionCoordinator txCoord = new BatchModeTransactionCoordinator();
        when(session.getTransactionCoordinator()).thenReturn(txCoord);
        when(session.beginTransaction()).then(invocation -> {
            Transaction tx = txCoord.newTransaction();
            tx.begin();
            return tx;
        });
    } else if (jtaPlatform == null) {
        Connection connection = mock(Connection.class);
        JdbcConnectionAccess jdbcConnectionAccess = mock(JdbcConnectionAccess.class);
        try {
            when(jdbcConnectionAccess.obtainConnection()).thenReturn(connection);
        } catch (SQLException e) {
        // never thrown from mock
        }
        JdbcSessionOwner jdbcSessionOwner = mock(JdbcSessionOwner.class);
        when(jdbcSessionOwner.getJdbcConnectionAccess()).thenReturn(jdbcConnectionAccess);
        SqlExceptionHelper sqlExceptionHelper = mock(SqlExceptionHelper.class);
        JdbcServices jdbcServices = mock(JdbcServices.class);
        when(jdbcServices.getSqlExceptionHelper()).thenReturn(sqlExceptionHelper);
        ServiceRegistry serviceRegistry = mock(ServiceRegistry.class);
        when(serviceRegistry.getService(JdbcServices.class)).thenReturn(jdbcServices);
        JdbcSessionContext jdbcSessionContext = mock(JdbcSessionContext.class);
        when(jdbcSessionContext.getServiceRegistry()).thenReturn(serviceRegistry);
        when(jdbcSessionOwner.getJdbcSessionContext()).thenReturn(jdbcSessionContext);
        NonJtaTransactionCoordinator txOwner = mock(NonJtaTransactionCoordinator.class);
        when(txOwner.getResourceLocalTransaction()).thenReturn(new JdbcResourceTransactionMock());
        when(txOwner.getJdbcSessionOwner()).thenReturn(jdbcSessionOwner);
        when(txOwner.isActive()).thenReturn(true);
        TransactionCoordinator txCoord = JdbcResourceLocalTransactionCoordinatorBuilderImpl.INSTANCE.buildTransactionCoordinator(txOwner, null);
        when(session.getTransactionCoordinator()).thenReturn(txCoord);
        when(session.beginTransaction()).then(invocation -> {
            Transaction tx = new TransactionImpl(txCoord, session.getExceptionConverter());
            tx.begin();
            return tx;
        });
    } else {
        throw new IllegalStateException("Unknown JtaPlatform: " + jtaPlatform);
    }
    return session;
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) BatchModeJtaPlatform(org.hibernate.test.cache.infinispan.util.BatchModeJtaPlatform) Cache(org.infinispan.Cache) Transaction(org.hibernate.Transaction) AdvancedCache(org.infinispan.AdvancedCache) After(org.junit.After) Mockito.doAnswer(org.mockito.Mockito.doAnswer) CacheDataDescriptionImpl(org.hibernate.cache.internal.CacheDataDescriptionImpl) TestingUtil(org.infinispan.test.TestingUtil) AccessType(org.hibernate.cache.spi.access.AccessType) Predicate(java.util.function.Predicate) TombstoneUpdate(org.hibernate.cache.infinispan.util.TombstoneUpdate) JdbcResourceTransactionAccess(org.hibernate.resource.transaction.backend.jdbc.spi.JdbcResourceTransactionAccess) TestSynchronization(org.hibernate.test.cache.infinispan.util.TestSynchronization) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) SoftLock(org.hibernate.cache.spi.access.SoftLock) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) AfterClassOnce(org.hibernate.testing.AfterClassOnce) FutureUpdate(org.hibernate.cache.infinispan.util.FutureUpdate) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) TransactionImpl(org.hibernate.engine.transaction.internal.TransactionImpl) PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Mockito.mock(org.mockito.Mockito.mock) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) TestResourceTracker(org.infinispan.test.fwk.TestResourceTracker) Logger(org.jboss.logging.Logger) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) Session(org.hibernate.Session) Caches(org.hibernate.cache.infinispan.util.Caches) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) RegionAccessStrategy(org.hibernate.cache.spi.access.RegionAccessStrategy) SQLException(java.sql.SQLException) TestTimeService(org.hibernate.test.cache.infinispan.util.TestTimeService) RollbackException(javax.transaction.RollbackException) TransactionCoordinatorOwner(org.hibernate.resource.transaction.spi.TransactionCoordinatorOwner) ExpectingInterceptor(org.hibernate.test.cache.infinispan.util.ExpectingInterceptor) ComparableComparator(org.hibernate.internal.util.compare.ComparableComparator) BatchModeTransactionCoordinator(org.hibernate.test.cache.infinispan.util.BatchModeTransactionCoordinator) JdbcResourceTransactionMock(org.hibernate.test.cache.infinispan.util.JdbcResourceTransactionMock) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AssertionFailedError(junit.framework.AssertionFailedError) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) Mockito.when(org.mockito.Mockito.when) BaseRegion(org.hibernate.cache.infinispan.impl.BaseRegion) ServiceRegistry(org.hibernate.service.ServiceRegistry) TimeUnit(java.util.concurrent.TimeUnit) CacheDataDescription(org.hibernate.cache.spi.CacheDataDescription) InvalidateCommand(org.infinispan.commands.write.InvalidateCommand) Assert.assertNull(org.junit.Assert.assertNull) SystemException(javax.transaction.SystemException) JdbcResourceLocalTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) BeforeClassOnce(org.hibernate.testing.BeforeClassOnce) Assert.assertEquals(org.junit.Assert.assertEquals) JdbcResourceTransactionMock(org.hibernate.test.cache.infinispan.util.JdbcResourceTransactionMock) JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) SQLException(java.sql.SQLException) Connection(java.sql.Connection) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) BatchModeTransactionCoordinator(org.hibernate.test.cache.infinispan.util.BatchModeTransactionCoordinator) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) TransactionImpl(org.hibernate.engine.transaction.internal.TransactionImpl) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) BatchModeTransactionCoordinator(org.hibernate.test.cache.infinispan.util.BatchModeTransactionCoordinator) Transaction(org.hibernate.Transaction) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) ServiceRegistry(org.hibernate.service.ServiceRegistry)

Example 2 with TransactionCoordinator

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

the class PutFromLoadValidator method registerRemoteInvalidation.

public boolean registerRemoteInvalidation(Object key, Object lockOwner) {
    SharedSessionContractImplementor session = currentSession.get();
    TransactionCoordinator transactionCoordinator = session == null ? null : session.getTransactionCoordinator();
    if (transactionCoordinator != null) {
        if (trace) {
            log.tracef("Registering synchronization on transaction in %s, cache %s: %s", lockOwnerToString(session), cache.getName(), key);
        }
        InvalidationSynchronization sync = new InvalidationSynchronization(nonTxPutFromLoadInterceptor, key, lockOwner);
        transactionCoordinator.getLocalSynchronizations().registerSynchronization(sync);
        return true;
    }
    // evict() command is not executed in session context
    return false;
}
Also used : TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor)

Example 3 with TransactionCoordinator

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

the class NonStrictAccessDelegate method remove.

@Override
public void remove(SharedSessionContractImplementor session, Object key) throws CacheException {
    // there's no 'afterRemove', so we have to use our own synchronization
    // the API does not provide version of removed item but we can't load it from the cache
    // as that would be prone to race conditions - if the entry was updated in the meantime
    // the remove could be discarded and we would end up with stale record
    // See VersionedTest#testCollectionUpdate for such situation
    TransactionCoordinator transactionCoordinator = session.getTransactionCoordinator();
    RemovalSynchronization sync = new RemovalSynchronization(transactionCoordinator, writeCache, false, region, key);
    transactionCoordinator.getLocalSynchronizations().registerSynchronization(sync);
}
Also used : TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator)

Example 4 with TransactionCoordinator

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

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

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