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);
}
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;
}
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;
}
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();
}
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());
}
}
Aggregations