Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
import org.hibernate.action.spi.Executable;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.spi.CacheKey;
import org.hibernate.cache.spi.CollectionCacheKey;
import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
import org.hibernate.cache.spi.access.SoftLock;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionImplementor;
Expand Down Expand Up @@ -76,12 +77,14 @@ public final void beforeExecutions() throws CacheException {
// bidirectional association and it is one of the earlier entity actions which actually updates
// the database (this action is responsible for second-level cache invalidation only)
if ( persister.hasCache() ) {
final CacheKey ck = session.generateCacheKey(
final CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy();
final CollectionCacheKey ck = cache.generateCacheKey(
key,
persister.getKeyType(),
persister.getRole()
persister,
session.getFactory(),
session.getTenantIdentifier()
);
final SoftLock lock = persister.getCacheAccessStrategy().lockItem( ck, null );
final SoftLock lock = cache.lockItem( ck, null );
// the old behavior used key as opposed to getKey()
afterTransactionProcess = new CacheCleanupProcess( key, persister, lock );
}
Expand Down Expand Up @@ -127,12 +130,14 @@ protected final SessionImplementor getSession() {

protected final void evict() throws CacheException {
if ( persister.hasCache() ) {
final CacheKey ck = session.generateCacheKey(
final CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy();
final CollectionCacheKey ck = cache.generateCacheKey(
key,
persister.getKeyType(),
persister.getRole()
persister,
session.getFactory(),
session.getTenantIdentifier()
);
persister.getCacheAccessStrategy().remove( ck );
cache.remove( ck );
}
}

Expand Down Expand Up @@ -169,12 +174,14 @@ private CacheCleanupProcess(Serializable key, CollectionPersister persister, Sof

@Override
public void doAfterTransactionCompletion(boolean success, SessionImplementor session) {
final CacheKey ck = session.generateCacheKey(
final CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy();
final CollectionCacheKey ck = cache.generateCacheKey(
key,
persister.getKeyType(),
persister.getRole()
persister,
session.getFactory(),
session.getTenantIdentifier()
);
persister.getCacheAccessStrategy().unlockItem( ck, lock );
cache.unlockItem( ck, lock );
}
}

Expand All @@ -191,8 +198,3 @@ protected EventSource eventSource() {
}
}






Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.cache.spi.CacheKey;
import org.hibernate.cache.spi.EntityCacheKey;
import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
import org.hibernate.cache.spi.access.SoftLock;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.PersistenceContext;
Expand Down Expand Up @@ -84,10 +85,11 @@ public void execute() throws HibernateException {
version = persister.getVersion( instance );
}

final CacheKey ck;
final EntityCacheKey ck;
if ( persister.hasCache() ) {
ck = session.generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() );
lock = persister.getCacheAccessStrategy().lockItem( ck, version );
final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
ck = cache.generateCacheKey( id, persister, session.getFactory(), session.getTenantIdentifier() );
lock = cache.lockItem( ck, version );
}
else {
ck = null;
Expand Down Expand Up @@ -184,13 +186,16 @@ private void postCommitDelete(boolean success) {

@Override
public void doAfterTransactionCompletion(boolean success, SessionImplementor session) throws HibernateException {
if ( getPersister().hasCache() ) {
final CacheKey ck = getSession().generateCacheKey(
EntityPersister entityPersister = getPersister();
if ( entityPersister.hasCache() ) {
EntityRegionAccessStrategy cache = entityPersister.getCacheAccessStrategy();
final EntityCacheKey ck = cache.generateCacheKey(
getId(),
getPersister().getIdentifierType(),
getPersister().getRootEntityName()
entityPersister,
session.getFactory(),
session.getTenantIdentifier()
);
getPersister().getCacheAccessStrategy().unlockItem( ck, lock );
cache.unlockItem( ck, lock );
}
postCommitDelete( success );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.cache.spi.CacheKey;
import org.hibernate.cache.spi.EntityCacheKey;
import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
import org.hibernate.cache.spi.entry.CacheEntry;
import org.hibernate.engine.internal.Versioning;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SessionEventListenerManager;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.event.service.spi.EventListenerGroup;
Expand Down Expand Up @@ -85,8 +88,8 @@ public void execute() throws HibernateException {
if ( !veto ) {

persister.insert( id, getState(), instance, session );

final EntityEntry entry = session.getPersistenceContext().getEntry( instance );
PersistenceContext persistenceContext = session.getPersistenceContext();
final EntityEntry entry = persistenceContext.getEntry( instance );
if ( entry == null ) {
throw new AssertionFailure( "possible non-threadsafe access to session" );
}
Expand All @@ -101,10 +104,10 @@ public void execute() throws HibernateException {
entry.postUpdate( instance, getState(), version );
}

getSession().getPersistenceContext().registerInsertedKey( getPersister(), getId() );
persistenceContext.registerInsertedKey( persister, getId() );
}

final SessionFactoryImplementor factory = getSession().getFactory();
final SessionFactoryImplementor factory = session.getFactory();

if ( isCachePutEnabled( persister, session ) ) {
final CacheEntry ce = persister.buildCacheEntry(
Expand All @@ -114,12 +117,13 @@ public void execute() throws HibernateException {
session
);
cacheEntry = persister.getCacheEntryStructure().structure( ce );
final CacheKey ck = session.generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() );
final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
final EntityCacheKey ck = cache.generateCacheKey( id, persister, factory, session.getTenantIdentifier() );

final boolean put = cacheInsert( persister, ck );

if ( put && factory.getStatistics().isStatisticsEnabled() ) {
factory.getStatisticsImplementor().secondLevelCachePut( getPersister().getCacheAccessStrategy().getRegion().getName() );
factory.getStatisticsImplementor().secondLevelCachePut( cache.getRegion().getName() );
}
}

Expand All @@ -134,7 +138,7 @@ public void execute() throws HibernateException {
markExecuted();
}

private boolean cacheInsert(EntityPersister persister, CacheKey ck) {
private boolean cacheInsert(EntityPersister persister, EntityCacheKey ck) {
try {
getSession().getEventListenerManager().cachePutStart();
return persister.getCacheAccessStrategy().insert( ck, cacheEntry, version );
Expand Down Expand Up @@ -207,24 +211,27 @@ private boolean preInsert() {
public void doAfterTransactionCompletion(boolean success, SessionImplementor session) throws HibernateException {
final EntityPersister persister = getPersister();
if ( success && isCachePutEnabled( persister, getSession() ) ) {
final CacheKey ck = getSession().generateCacheKey( getId(), persister.getIdentifierType(), persister.getRootEntityName() );
final boolean put = cacheAfterInsert( persister, ck );

if ( put && getSession().getFactory().getStatistics().isStatisticsEnabled() ) {
getSession().getFactory().getStatisticsImplementor()
.secondLevelCachePut( getPersister().getCacheAccessStrategy().getRegion().getName() );
final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
SessionFactoryImplementor sessionFactoryImplementor = session.getFactory();
final EntityCacheKey ck = cache.generateCacheKey( getId(), persister, sessionFactoryImplementor, session.getTenantIdentifier() );
final boolean put = cacheAfterInsert( cache, ck );

if ( put && sessionFactoryImplementor.getStatistics().isStatisticsEnabled() ) {
sessionFactoryImplementor.getStatisticsImplementor()
.secondLevelCachePut( cache.getRegion().getName() );
}
}
postCommitInsert( success );
}

private boolean cacheAfterInsert(EntityPersister persister, CacheKey ck) {
private boolean cacheAfterInsert(EntityRegionAccessStrategy cache, EntityCacheKey ck) {
final SessionEventListenerManager eventListenerManager = getSession().getEventListenerManager();
try {
getSession().getEventListenerManager().cachePutStart();
return persister.getCacheAccessStrategy().afterInsert( ck, cacheEntry, version );
eventListenerManager.cachePutStart();
return cache.afterInsert( ck, cacheEntry, version );
}
finally {
getSession().getEventListenerManager().cachePutEnd();
eventListenerManager.cachePutEnd();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.cache.CacheException;
import org.hibernate.cache.spi.CacheKey;
import org.hibernate.cache.spi.EntityCacheKey;
import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
import org.hibernate.cache.spi.access.SoftLock;
import org.hibernate.cache.spi.entry.CacheEntry;
import org.hibernate.engine.internal.Versioning;
import org.hibernate.engine.spi.CachedNaturalIdValueSource;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.SessionEventListenerManager;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.Status;
Expand Down Expand Up @@ -116,7 +118,7 @@ public void execute() throws HibernateException {

final boolean veto = preUpdate();

final SessionFactoryImplementor factory = getSession().getFactory();
final SessionFactoryImplementor factory = session.getFactory();
Object previousVersion = this.previousVersion;
if ( persister.isVersionPropertyGenerated() ) {
// we need to grab the version value from the entity, otherwise
Expand All @@ -125,14 +127,16 @@ public void execute() throws HibernateException {
previousVersion = persister.getVersion( instance );
}

final CacheKey ck;
final EntityCacheKey ck;
if ( persister.hasCache() ) {
ck = session.generateCacheKey(
final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
ck = cache.generateCacheKey(
id,
persister.getIdentifierType(),
persister.getRootEntityName()
persister,
factory,
session.getTenantIdentifier()
);
lock = persister.getCacheAccessStrategy().lockItem( ck, previousVersion );
lock = cache.lockItem( ck, previousVersion );
}
else {
ck = null;
Expand All @@ -152,7 +156,7 @@ public void execute() throws HibernateException {
);
}

final EntityEntry entry = getSession().getPersistenceContext().getEntry( instance );
final EntityEntry entry = session.getPersistenceContext().getEntry( instance );
if ( entry == null ) {
throw new AssertionFailure( "possible nonthreadsafe access to session" );
}
Expand Down Expand Up @@ -212,7 +216,7 @@ public void execute() throws HibernateException {
}
}

private boolean cacheUpdate(EntityPersister persister, Object previousVersion, CacheKey ck) {
private boolean cacheUpdate(EntityPersister persister, Object previousVersion, EntityCacheKey ck) {
try {
getSession().getEventListenerManager().cachePutStart();
return persister.getCacheAccessStrategy().update( ck, cacheEntry, nextVersion, previousVersion );
Expand Down Expand Up @@ -307,34 +311,37 @@ protected boolean hasPostCommitEventListeners() {
public void doAfterTransactionCompletion(boolean success, SessionImplementor session) throws CacheException {
final EntityPersister persister = getPersister();
if ( persister.hasCache() ) {

final CacheKey ck = getSession().generateCacheKey(
final EntityRegionAccessStrategy cache = persister.getCacheAccessStrategy();
final EntityCacheKey ck = cache.generateCacheKey(
getId(),
persister.getIdentifierType(),
persister.getRootEntityName()
persister,
session.getFactory(),
session.getTenantIdentifier()

);

if ( success && cacheEntry!=null /*!persister.isCacheInvalidationRequired()*/ ) {
final boolean put = cacheAfterUpdate( persister, ck );
final boolean put = cacheAfterUpdate( cache, ck );

if ( put && getSession().getFactory().getStatistics().isStatisticsEnabled() ) {
getSession().getFactory().getStatisticsImplementor().secondLevelCachePut( getPersister().getCacheAccessStrategy().getRegion().getName() );
getSession().getFactory().getStatisticsImplementor().secondLevelCachePut( cache.getRegion().getName() );
}
}
else {
persister.getCacheAccessStrategy().unlockItem( ck, lock );
cache.unlockItem( ck, lock );
}
}
postCommitUpdate( success );
}

private boolean cacheAfterUpdate(EntityPersister persister, CacheKey ck) {
private boolean cacheAfterUpdate(EntityRegionAccessStrategy cache, EntityCacheKey ck) {
SessionEventListenerManager eventListenerManager = getSession().getEventListenerManager();
try {
getSession().getEventListenerManager().cachePutStart();
return persister.getCacheAccessStrategy().afterUpdate( ck, cacheEntry, nextVersion, previousVersion, lock );
eventListenerManager.cachePutStart();
return cache.afterUpdate( ck, cacheEntry, nextVersion, previousVersion, lock );
}
finally {
getSession().getEventListenerManager().cachePutEnd();
eventListenerManager.cachePutEnd();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import java.util.Set;

import org.hibernate.boot.Metadata;
import org.hibernate.cache.spi.CacheKey;
import org.hibernate.cache.spi.CollectionCacheKey;
import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventSource;
Expand Down Expand Up @@ -136,8 +137,16 @@ private void evictCache(Object entity, EntityPersister persister, EventSource se
}

private void evict(Serializable id, CollectionPersister collectionPersister, EventSource session) {
LOG.debug( "Evict CollectionRegion " + collectionPersister.getRole() + " for id " + id );
CacheKey key = session.generateCacheKey( id, collectionPersister.getKeyType(), collectionPersister.getRole() );
collectionPersister.getCacheAccessStrategy().evict( key );
if ( LOG.isDebugEnabled() ) {
LOG.debug( "Evict CollectionRegion " + collectionPersister.getRole() + " for id " + id );
}
CollectionRegionAccessStrategy cache = collectionPersister.getCacheAccessStrategy();
CollectionCacheKey key = cache.generateCacheKey(
id,
collectionPersister,
session.getFactory(),
session.getTenantIdentifier()
);
cache.evict( key );
}
}
Loading