@Override public boolean containsCollection(String role, Serializable ownerIdentifier) { final CollectionPersister collectionDescriptor = sessionFactory.getMetamodel() .collectionPersister( role ); final CollectionDataAccess cacheAccess = collectionDescriptor.getCacheAccessStrategy(); if ( cacheAccess == null ) { return false; } final Object key = cacheAccess.generateCacheKey( ownerIdentifier, collectionDescriptor, sessionFactory, null ); return cacheAccess.contains( key ); }
@Override public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) { final CollectionDataAccess cache = persister.getCacheAccessStrategy(); final Object ck = cache.generateCacheKey( key, persister, session.getFactory(), session.getTenantIdentifier() ); cache.unlockItem( session, ck, lock ); } }
protected final void evict() throws CacheException { if ( persister.hasCache() ) { final CollectionDataAccess cache = persister.getCacheAccessStrategy(); final Object ck = cache.generateCacheKey( key, persister, session.getFactory(), session.getTenantIdentifier() ); cache.remove( session, ck); } }
@Override public final void beforeExecutions() throws CacheException { // we need to obtain the lock before any actions are executed, since this may be an inverse="true" // 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 CollectionDataAccess cache = persister.getCacheAccessStrategy(); final Object ck = cache.generateCacheKey( key, persister, session.getFactory(), session.getTenantIdentifier() ); final SoftLock lock = cache.lockItem( session, ck, null ); // the old behavior used key as opposed to getKey() afterTransactionProcess = new CacheCleanupProcess( key, persister, lock ); } }
@Override public void evictCollectionData(String role, Serializable ownerIdentifier) { final CollectionPersister collectionDescriptor = sessionFactory.getMetamodel() .collectionPersister( role ); final CollectionDataAccess cacheAccess = collectionDescriptor.getCacheAccessStrategy(); if ( cacheAccess == null ) { return; } if ( LOG.isDebugEnabled() ) { LOG.debugf( "Evicting second-level cache: %s", MessageHelper.collectionInfoString( collectionDescriptor, ownerIdentifier, sessionFactory ) ); } final Object key = cacheAccess.generateCacheKey( ownerIdentifier, collectionDescriptor, sessionFactory, null ); cacheAccess.evict( key ); }
private boolean isCached(Serializable collectionKey, CollectionPersister persister) { SharedSessionContractImplementor session = context.getSession(); if ( session.getCacheMode().isGetEnabled() && persister.hasCache() ) { CollectionDataAccess cache = persister.getCacheAccessStrategy(); Object cacheKey = cache.generateCacheKey( collectionKey, persister, session.getFactory(), session.getTenantIdentifier() ); return CacheHelper.fromSharedCache( session, cacheKey, cache ) != null; } return false; }
@Test public void testCachedValueAfterEviction() { CollectionPersister persister = sessionFactory().getCollectionPersister( Company.class.getName() + ".users" ); Session session = openSession(); SessionImplementor sessionImplementor = (SessionImplementor) session; CollectionDataAccess cache = persister.getCacheAccessStrategy(); Object key = cache.generateCacheKey( 1, persister, sessionFactory(), session.getTenantIdentifier() ); Object cachedValue = cache.get( sessionImplementor, key ); assertNull( cachedValue ); Company company = session.get( Company.class, 1 ); //should add in cache assertEquals( 1, company.getUsers().size() ); session.close(); session = openSession(); sessionImplementor = (SessionImplementor) session; key = cache.generateCacheKey( 1, persister, sessionFactory(), session.getTenantIdentifier() ); cachedValue = cache.get( sessionImplementor, key ); assertNotNull( "Collection wasn't cached", cachedValue ); session.close(); }
private void evictCachedCollections(Type[] types, Serializable id, EventSource source) throws HibernateException { for ( Type type : types ) { if ( type.isCollectionType() ) { CollectionPersister collectionPersister = source.getFactory().getMetamodel().collectionPersister( ( (CollectionType) type ).getRole() ); if ( collectionPersister.hasCache() ) { final CollectionDataAccess cache = collectionPersister.getCacheAccessStrategy(); final Object ck = cache.generateCacheKey( id, collectionPersister, source.getFactory(), source.getTenantIdentifier() ); final SoftLock lock = cache.lockItem( source, ck, null ); cache.remove( source, ck ); source.getActionQueue().registerProcess( (success, session) -> cache.unlockItem( session, ck, lock ) ); } } else if ( type.isComponentType() ) { CompositeType actype = (CompositeType) type; evictCachedCollections( actype.getSubtypes(), id, source ); } } } }
final Object ck = cacheAccessStrategy.generateCacheKey( id, persister, factory, source.getTenantIdentifier() ); final Object ce = CacheHelper.fromSharedCache( source, ck, persister.getCacheAccessStrategy() );
final Object cacheKey = cacheAccess.generateCacheKey( lce.getKey(), persister,
@Override public boolean containsCollection(String role, Serializable ownerIdentifier) { final PersistentCollectionDescriptor collectionDescriptor = sessionFactory.getMetamodel().findCollectionDescriptor( role ); final CollectionDataAccess cacheAccess = collectionDescriptor.getCacheAccess(); if ( cacheAccess == null ) { return false; } final Object key = cacheAccess.generateCacheKey( ownerIdentifier, collectionDescriptor, sessionFactory, null ); return cacheAccess.contains( key ); }
@Override public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) { final CollectionDataAccess cache = collectionDescriptor.getCacheAccess(); final Object ck = cache.generateCacheKey( key, collectionDescriptor, session.getFactory(), session.getTenantIdentifier() ); cache.unlockItem( session, ck, lock ); } }
protected final void evict() throws CacheException { if ( collectionDescriptor.hasCache() ) { final CollectionDataAccess cacheAccess = collectionDescriptor.getCacheAccess(); final Object ck = cacheAccess.generateCacheKey( key, collectionDescriptor, session.getFactory(), session.getTenantIdentifier() ); cacheAccess.remove( session, ck); } }
@Override public final void beforeExecutions() throws CacheException { // we need to obtain the lock before any actions are executed, since this may be an inverse="true" // 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 ( collectionDescriptor.hasCache() ) { final CollectionDataAccess cacheAccess = collectionDescriptor.getCacheAccess(); final Object ck = cacheAccess.generateCacheKey( key, collectionDescriptor, session.getFactory(), session.getTenantIdentifier() ); final SoftLock lock = cacheAccess.lockItem( session, ck, null ); // the old behavior used key as opposed to getKey() afterTransactionProcess = new CacheCleanupProcess( key, collectionDescriptor, lock ); } }
@Override public void evictCollectionData(String role, Serializable ownerIdentifier) { final PersistentCollectionDescriptor collectionDescriptor = sessionFactory.getMetamodel().findCollectionDescriptor( role ); final CollectionDataAccess cacheAccess = collectionDescriptor.getCacheAccess(); if ( cacheAccess == null ) { return; } if ( LOG.isDebugEnabled() ) { LOG.debugf( "Evicting second-level cache: %s", MessageHelper.collectionInfoString( collectionDescriptor, ownerIdentifier, sessionFactory ) ); } final Object key = cacheAccess.generateCacheKey( ownerIdentifier, collectionDescriptor, sessionFactory, null ); cacheAccess.evict( key ); }
private boolean isCached(Object collectionKey, PersistentCollectionDescriptor descriptor) { SharedSessionContractImplementor session = context.getSession(); if ( session.getCacheMode().isGetEnabled() && descriptor.hasCache() ) { CollectionDataAccess cache = descriptor.getCacheAccess(); Object cacheKey = cache.generateCacheKey( collectionKey, descriptor, session.getFactory(), session.getTenantIdentifier() ); return CacheHelper.fromSharedCache( session, cacheKey, cache ) != null; } return false; }
@SuppressWarnings("unchecked") private void evictCachedCollections(List<PersistentAttributeDescriptor> persistentAttributes, Object id, EventSource source) throws HibernateException { for ( PersistentAttributeDescriptor attribute : persistentAttributes ) { if ( PluralPersistentAttribute.class.isInstance( attribute ) ) { final PersistentCollectionDescriptor collectionDescriptor = ( (PluralPersistentAttribute) attribute ).getPersistentCollectionDescriptor(); if ( collectionDescriptor.hasCache() ) { final CollectionDataAccess cache = collectionDescriptor.getCacheAccess(); final Object ck = cache.generateCacheKey( id, collectionDescriptor, source.getFactory(), source.getTenantIdentifier() ); final SoftLock lock = cache.lockItem( source, ck, null ); source.getActionQueue().registerProcess( (success, session) -> cache.unlockItem( session, ck, lock ) ); cache.remove( source, ck ); } } else if ( EmbeddedValuedNavigable.class.isInstance( attribute ) ) { EmbeddedValuedNavigable composite = (EmbeddedValuedNavigable) attribute; evictCachedCollections( composite.getEmbeddedDescriptor().getPersistentAttributes(), id, source ); } } } }
final Object ck = cacheAccess.generateCacheKey( collectionKey, collectionDescriptor, factory, source.getTenantIdentifier() ); final Object ce = CacheHelper.fromSharedCache( source, ck, cacheAccess );