Search in sources :

Example 6 with SharedSessionContractImplementor

use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.

the class PutFromLoadValidatorUnitTest method testGetForNullReleasePuts.

@Test
@TestForIssue(jiraKey = "HHH-9928")
public void testGetForNullReleasePuts() {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.simpleCache(true).expiration().maxIdle(500);
    Configuration ppCfg = cb.build();
    InfinispanRegionFactory regionFactory = mock(InfinispanRegionFactory.class);
    doReturn(ppCfg).when(regionFactory).getPendingPutsCacheConfiguration();
    doAnswer(invocation -> TIME_SERVICE.wallClockTime()).when(regionFactory).nextTimestamp();
    PutFromLoadValidator testee = new PutFromLoadValidator(cache, regionFactory, cm);
    for (int i = 0; i < 100; ++i) {
        try {
            withTx(tm, () -> {
                SharedSessionContractImplementor session = mock(SharedSessionContractImplementor.class);
                testee.registerPendingPut(session, KEY1, 0);
                return null;
            });
            TIME_SERVICE.advance(10);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    String ppName = cm.getCache().getName() + "-" + InfinispanRegionFactory.DEF_PENDING_PUTS_RESOURCE;
    Map ppCache = cm.getCache(ppName, false);
    assertNotNull(ppCache);
    Object pendingPutMap = ppCache.get(KEY1);
    assertNotNull(pendingPutMap);
    int size;
    try {
        Method sizeMethod = pendingPutMap.getClass().getMethod("size");
        sizeMethod.setAccessible(true);
        size = (Integer) sizeMethod.invoke(pendingPutMap);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    // some of the pending puts need to be expired by now
    assertTrue(size < 100);
    // but some are still registered
    assertTrue(size > 0);
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) Configuration(org.infinispan.configuration.cache.Configuration) PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Method(java.lang.reflect.Method) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException) Exceptions.expectException(org.infinispan.test.Exceptions.expectException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 7 with SharedSessionContractImplementor

use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.

the class AuditProcessManager method get.

public AuditProcess get(EventSource session) {
    final Transaction transaction = session.accessTransaction();
    AuditProcess auditProcess = auditProcesses.get(transaction);
    if (auditProcess == null) {
        // No worries about registering a transaction twice - a transaction is single thread
        auditProcess = new AuditProcess(revisionInfoGenerator, session);
        auditProcesses.put(transaction, auditProcess);
        session.getActionQueue().registerProcess(new BeforeTransactionCompletionProcess() {

            public void doBeforeTransactionCompletion(SessionImplementor session) {
                final AuditProcess process = auditProcesses.get(transaction);
                if (process != null) {
                    process.doBeforeTransactionCompletion(session);
                }
            }
        });
        session.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() {

            public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
                auditProcesses.remove(transaction);
            }
        });
    }
    return auditProcess;
}
Also used : BeforeTransactionCompletionProcess(org.hibernate.action.spi.BeforeTransactionCompletionProcess) Transaction(org.hibernate.Transaction) AfterTransactionCompletionProcess(org.hibernate.action.spi.AfterTransactionCompletionProcess) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor)

Example 8 with SharedSessionContractImplementor

use of org.hibernate.engine.spi.SharedSessionContractImplementor 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 9 with SharedSessionContractImplementor

use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.

the class EntityInsertAction method execute.

@Override
public void execute() throws HibernateException {
    nullifyTransientReferencesIfNotAlready();
    final EntityPersister persister = getPersister();
    final SharedSessionContractImplementor session = getSession();
    final Object instance = getInstance();
    final Serializable id = getId();
    final boolean veto = preInsert();
    if (!veto) {
        persister.insert(id, getState(), instance, session);
        PersistenceContext persistenceContext = session.getPersistenceContext();
        final EntityEntry entry = persistenceContext.getEntry(instance);
        if (entry == null) {
            throw new AssertionFailure("possible non-threadsafe access to session");
        }
        entry.postInsert(getState());
        if (persister.hasInsertGeneratedProperties()) {
            persister.processInsertGeneratedProperties(id, instance, getState(), session);
            if (persister.isVersionPropertyGenerated()) {
                version = Versioning.getVersion(getState(), persister);
            }
            entry.postUpdate(instance, getState(), version);
        }
        persistenceContext.registerInsertedKey(persister, getId());
    }
    final SessionFactoryImplementor factory = session.getFactory();
    if (isCachePutEnabled(persister, session)) {
        final CacheEntry ce = persister.buildCacheEntry(instance, getState(), version, session);
        cacheEntry = persister.getCacheEntryStructure().structure(ce);
        final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
        final Object ck = cache.generateCacheKey(id, persister, factory, session.getTenantIdentifier());
        final boolean put = cacheInsert(persister, ck);
        if (put && factory.getStatistics().isStatisticsEnabled()) {
            factory.getStatistics().secondLevelCachePut(cache.getRegion().getName());
        }
    }
    handleNaturalIdPostSaveNotifications(id);
    postInsert();
    if (factory.getStatistics().isStatisticsEnabled() && !veto) {
        factory.getStatistics().insertEntity(getPersister().getEntityName());
    }
    markExecuted();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable) EntityEntry(org.hibernate.engine.spi.EntityEntry) AssertionFailure(org.hibernate.AssertionFailure) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) CacheEntry(org.hibernate.cache.spi.entry.CacheEntry)

Example 10 with SharedSessionContractImplementor

use of org.hibernate.engine.spi.SharedSessionContractImplementor in project hibernate-orm by hibernate.

the class EntityDeleteAction method execute.

@Override
public void execute() throws HibernateException {
    final Serializable id = getId();
    final EntityPersister persister = getPersister();
    final SharedSessionContractImplementor session = getSession();
    final Object instance = getInstance();
    final boolean veto = preDelete();
    Object version = this.version;
    if (persister.isVersionPropertyGenerated()) {
        // we need to grab the version value from the entity, otherwise
        // we have issues with generated-version entities that may have
        // multiple actions queued during the same flush
        version = persister.getVersion(instance);
    }
    final Object ck;
    if (persister.hasCache()) {
        final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
        ck = cache.generateCacheKey(id, persister, session.getFactory(), session.getTenantIdentifier());
        lock = cache.lockItem(session, ck, version);
    } else {
        ck = null;
    }
    if (!isCascadeDeleteEnabled && !veto) {
        persister.delete(id, version, instance, session);
    }
    //postDelete:
    // After actually deleting a row, record the fact that the instance no longer 
    // exists on the database (needed for identity-column key generation), and
    // remove it from the session cache
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final EntityEntry entry = persistenceContext.removeEntry(instance);
    if (entry == null) {
        throw new AssertionFailure("possible nonthreadsafe access to session");
    }
    entry.postDelete();
    persistenceContext.removeEntity(entry.getEntityKey());
    persistenceContext.removeProxy(entry.getEntityKey());
    if (persister.hasCache()) {
        persister.getCacheAccessStrategy().remove(session, ck);
    }
    persistenceContext.getNaturalIdHelper().removeSharedNaturalIdCrossReference(persister, id, naturalIdValues);
    postDelete();
    if (getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto) {
        getSession().getFactory().getStatistics().deleteEntity(getPersister().getEntityName());
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Serializable(java.io.Serializable) EntityEntry(org.hibernate.engine.spi.EntityEntry) AssertionFailure(org.hibernate.AssertionFailure) EntityRegionAccessStrategy(org.hibernate.cache.spi.access.EntityRegionAccessStrategy) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Aggregations

SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)53 Test (org.junit.Test)16 CountDownLatch (java.util.concurrent.CountDownLatch)12 Session (org.hibernate.Session)10 EntityManager (javax.persistence.EntityManager)7 PutFromLoadValidator (org.hibernate.cache.infinispan.access.PutFromLoadValidator)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 JtaTransactionCoordinatorImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)7 Serializable (java.io.Serializable)6 AssertionFailedError (junit.framework.AssertionFailedError)6 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 HibernateException (org.hibernate.HibernateException)5 AssertionFailure (org.hibernate.AssertionFailure)4 TestForIssue (org.hibernate.testing.TestForIssue)4 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 List (java.util.List)3 TimeUnit (java.util.concurrent.TimeUnit)3 Transaction (org.hibernate.Transaction)3