Java Code Examples for org.hibernate.cache.spi.access.EntityDataAccess#generateCacheKey()

The following examples show how to use org.hibernate.cache.spi.access.EntityDataAccess#generateCacheKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EnabledCaching.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void evictEntityData(String entityName, Serializable identifier) {
	final EntityPersister entityDescriptor = sessionFactory.getMetamodel().entityPersister( entityName );
	final EntityDataAccess cacheAccess = entityDescriptor.getCacheAccessStrategy();
	if ( cacheAccess == null ) {
		return;
	}

	if ( LOG.isDebugEnabled() ) {
		LOG.debugf(
				"Evicting second-level cache: %s",
				MessageHelper.infoString( entityDescriptor, identifier, sessionFactory )
		);
	}

	final Object key = cacheAccess.generateCacheKey( identifier, entityDescriptor, sessionFactory, null );
	cacheAccess.evict( key );
}
 
Example 2
Source File: EntityInsertAction.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) throws HibernateException {
	final EntityPersister persister = getPersister();
	if ( success && isCachePutEnabled( persister, getSession() ) ) {
		final EntityDataAccess cache = persister.getCacheAccessStrategy();
		SessionFactoryImplementor factory = session.getFactory();
		final Object ck = cache.generateCacheKey( getId(), persister, factory, session.getTenantIdentifier() );
		final boolean put = cacheAfterInsert( cache, ck );

		if ( put && factory.getStatistics().isStatisticsEnabled() ) {
			factory.getStatistics().entityCachePut(
					StatsHelper.INSTANCE.getRootEntityRole( persister ),
					cache.getRegion().getName()
			);
		}
	}
	postCommitInsert( success );
}
 
Example 3
Source File: BatchFetchQueue.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private boolean isCached(EntityKey entityKey, EntityPersister persister) {
	final SharedSessionContractImplementor session = context.getSession();
	if ( context.getSession().getCacheMode().isGetEnabled() && persister.canReadFromCache() ) {
		final EntityDataAccess cache = persister.getCacheAccessStrategy();
		final Object key = cache.generateCacheKey(
				entityKey.getIdentifier(),
				persister,
				session.getFactory(),
				session.getTenantIdentifier()
		);
		return CacheHelper.fromSharedCache( session, key, cache ) != null;
	}
	return false;
}
 
Example 4
Source File: EnabledCaching.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean containsEntity(String entityName, Serializable identifier) {
	final EntityPersister entityDescriptor = sessionFactory.getMetamodel().entityPersister( entityName );
	final EntityDataAccess cacheAccess = entityDescriptor.getCacheAccessStrategy();
	if ( cacheAccess == null ) {
		return false;
	}

	final Object key = cacheAccess.generateCacheKey( identifier, entityDescriptor, sessionFactory, null );
	return cacheAccess.contains( key );
}
 
Example 5
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If the class to be loaded has been configured with a cache, then lock
 * given id in that cache and then perform the load.
 *
 * @param event The initiating load request event
 * @param persister The persister corresponding to the entity to be loaded
 * @param keyToLoad The key of the entity to be loaded
 * @param options The defined load options
 * @param source The originating session
 *
 * @return The loaded entity
 *
 * @throws HibernateException
 */
private Object lockAndLoad(
		final LoadEvent event,
		final EntityPersister persister,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options,
		final SessionImplementor source) {
	SoftLock lock = null;
	final Object ck;
	final EntityDataAccess cache = persister.getCacheAccessStrategy();
	if ( persister.canWriteToCache() ) {
		ck = cache.generateCacheKey(
				event.getEntityId(),
				persister,
				source.getFactory(),
				source.getTenantIdentifier()
		);
		lock = persister.getCacheAccessStrategy().lockItem( source, ck, null );
	}
	else {
		ck = null;
	}

	Object entity;
	try {
		entity = load( event, persister, keyToLoad, options );
	}
	finally {
		if ( persister.canWriteToCache() ) {
			cache.unlockItem( source, ck, lock );
		}
	}

	return event.getSession().getPersistenceContext().proxyFor( persister, keyToLoad, entity );
}
 
Example 6
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Object getFromSharedCache(
	final LoadEvent event,
	final EntityPersister persister,
	SessionImplementor source ) {
	final EntityDataAccess cache = persister.getCacheAccessStrategy();
	final Object ck = cache.generateCacheKey(
			event.getEntityId(),
			persister,
			source.getFactory(),
			source.getTenantIdentifier()
	);

	final Object ce = CacheHelper.fromSharedCache( source, ck, persister.getCacheAccessStrategy() );
	if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
		if ( ce == null ) {
			source.getFactory().getStatistics().entityCacheMiss(
					StatsHelper.INSTANCE.getRootEntityRole( persister ),
					cache.getRegion().getName()
			);
		}
		else {
			source.getFactory().getStatistics().entityCacheHit(
					StatsHelper.INSTANCE.getRootEntityRole( persister ),
					cache.getRegion().getName()
			);
		}
	}
	return ce;
}
 
Example 7
Source File: EntityDeleteAction.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) throws HibernateException {
	EntityPersister entityPersister = getPersister();
	if ( entityPersister.canWriteToCache() ) {
		EntityDataAccess cache = entityPersister.getCacheAccessStrategy();
		final Object ck = cache.generateCacheKey(
				getId(),
				entityPersister,
				session.getFactory(),
				session.getTenantIdentifier()
		);
		cache.unlockItem( session, ck, lock );
	}
	postCommitDelete( success );
}
 
Example 8
Source File: EntityUpdateAction.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) throws CacheException {
	final EntityPersister persister = getPersister();
	if ( persister.canWriteToCache() ) {
		final EntityDataAccess cache = persister.getCacheAccessStrategy();
		final Object ck = cache.generateCacheKey(
				getId(),
				persister,
				session.getFactory(),
				session.getTenantIdentifier()
				
		);

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

			if ( put && getSession().getFactory().getStatistics().isStatisticsEnabled() ) {
				session.getFactory().getStatistics().entityCachePut(
						StatsHelper.INSTANCE.getRootEntityRole( persister ),
						getPersister().getCacheAccessStrategy().getRegion().getName()
				);
			}
		}
		else {
			cache.unlockItem(session, ck, lock );
		}
	}
	postCommitUpdate( success );
}
 
Example 9
Source File: ReactiveEntityDeleteAction.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public CompletionStage<Void> reactiveExecute() throws HibernateException {
	final Serializable id = getId();
	final EntityPersister persister = getPersister();
	final SharedSessionContractImplementor session = getSession();
	final Object instance = getInstance();

	final boolean veto = preDelete();

	Object version = getVersion();
	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.canWriteToCache() ) {
		final EntityDataAccess cache = persister.getCacheAccessStrategy();
		ck = cache.generateCacheKey( id, persister, session.getFactory(), session.getTenantIdentifier() );
		setLock( cache.lockItem( session, ck, version ) );
	}
	else {
		ck = null;
	}

	CompletionStage<?> deleteStep = CompletionStages.nullFuture();
	if ( !isCascadeDeleteEnabled() && !veto ) {
		deleteStep = ((ReactiveEntityPersister) persister).deleteReactive( id, version, instance, session );
	}

	return deleteStep.thenAccept( deleteAR -> {
		//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.getPersistenceContextInternal();
		final EntityEntry entry = persistenceContext.removeEntry( instance );
		if ( entry == null ) {
			throw new AssertionFailure( "possible non-threadsafe access to session" );
		}
		entry.postDelete();

		persistenceContext.removeEntity( entry.getEntityKey() );
		persistenceContext.removeProxy( entry.getEntityKey() );

		if ( persister.canWriteToCache() ) {
			persister.getCacheAccessStrategy().remove( session, ck );
		}

		persistenceContext.getNaturalIdHelper().removeSharedNaturalIdCrossReference(
				persister,
				id,
				getNaturalIdValues()
		);

		postDelete();

		final StatisticsImplementor statistics = getSession().getFactory().getStatistics();
		if ( statistics.isStatisticsEnabled() && !veto ) {
			statistics.deleteEntity( getPersister().getEntityName() );
		}
	} );
}
 
Example 10
Source File: DefaultReactiveLoadEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * If the class to be loaded has been configured with a cache, then lock
 * given id in that cache and then perform the load.
 *
 * @param event The initiating load request event
 * @param persister The persister corresponding to the entity to be loaded
 * @param keyToLoad The key of the entity to be loaded
 * @param options The defined load options
 * @param source The originating session
 *
 * @return The loaded entity
 */
private CompletionStage<Object> lockAndLoad(
		final LoadEvent event,
		final EntityPersister persister,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options,
		final SessionImplementor source) {
	final SoftLock lock;
	final Object ck;
	final EntityDataAccess cache = persister.getCacheAccessStrategy();
	final boolean canWriteToCache = persister.canWriteToCache();
	if ( canWriteToCache ) {
		ck = cache.generateCacheKey(
				event.getEntityId(),
				persister,
				source.getFactory(),
				source.getTenantIdentifier()
		);
		lock = cache.lockItem( source, ck, null );
	}
	else {
		ck = null;
		lock = null;
	}

	try {
		return load( event, persister, keyToLoad, options )
				.whenComplete( (v, x) -> {
					if ( canWriteToCache ) {
						cache.unlockItem( source, ck, lock );
					}
				} )
				.thenApply( entity -> source.getPersistenceContextInternal().proxyFor( persister, keyToLoad, entity ) );
	}
	catch (HibernateException he) {
		//in case load() throws an exception
		if ( canWriteToCache ) {
			cache.unlockItem( source, ck, lock );
		}
		throw he;
	}
}
 
Example 11
Source File: DefaultReactiveLockEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Performs a pessimistic lock upgrade on a given entity, if needed.
 *
 * @param object The entity for which to upgrade the lock.
 * @param entry The entity's EntityEntry instance.
 * @param lockOptions contains the requested lock mode.
 * @param source The session which is the source of the event being processed.
 */
protected CompletionStage<Void> upgradeLock(Object object, EntityEntry entry,
											LockOptions lockOptions,
											EventSource source) {

	LockMode requestedLockMode = lockOptions.getLockMode();
	if ( requestedLockMode.greaterThan( entry.getLockMode() ) ) {
		// The user requested a "greater" (i.e. more restrictive) form of
		// pessimistic lock

		if ( entry.getStatus() != Status.MANAGED ) {
			throw new ObjectDeletedException(
					"attempted to lock a deleted instance",
					entry.getId(),
					entry.getPersister().getEntityName()
			);
		}

		final EntityPersister persister = entry.getPersister();

		if ( log.isTraceEnabled() ) {
			log.tracev(
					"Locking {0} in mode: {1}",
					MessageHelper.infoString( persister, entry.getId(), source.getFactory() ),
					requestedLockMode
			);
		}

		final boolean cachingEnabled = persister.canWriteToCache();
		final SoftLock lock;
		final Object ck;
		if ( cachingEnabled ) {
			EntityDataAccess cache = persister.getCacheAccessStrategy();
			ck = cache.generateCacheKey(
					entry.getId(),
					persister,
					source.getFactory(),
					source.getTenantIdentifier()
			);
			lock = cache.lockItem( source, ck, entry.getVersion() );
		}
		else {
			lock = null;
			ck = null;
		}

		return ((ReactiveEntityPersister) persister).lockReactive(
				entry.getId(),
				entry.getVersion(),
				object,
				lockOptions,
				source
		).thenAccept( v -> entry.setLockMode(requestedLockMode) )
				.whenComplete( (r, e) -> {
					// the database now holds a lock + the object is flushed from the cache,
					// so release the soft lock
					if ( cachingEnabled ) {
						persister.getCacheAccessStrategy().unlockItem( source, ck, lock );
					}
				} );

	}
	else {
		return CompletionStages.nullFuture();
	}
}
 
Example 12
Source File: Loader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The entity instance is not in the session cache
 */
private Object instanceNotYetLoaded(
		final ResultSet rs,
		final int i,
		final Loadable persister,
		final String rowIdAlias,
		final EntityKey key,
		final LockMode lockMode,
		final EntityKey optionalObjectKey,
		final Object optionalObject,
		final List hydratedObjects,
		final SharedSessionContractImplementor session)
		throws HibernateException, SQLException {
	final String instanceClass = getInstanceClass(
			rs,
			i,
			persister,
			key.getIdentifier(),
			session
	);

	// see if the entity defines reference caching, and if so use the cached reference (if one).
	if ( session.getCacheMode().isGetEnabled() && persister.canUseReferenceCacheEntries() ) {
		final EntityDataAccess cache = persister.getCacheAccessStrategy();
		final Object ck = cache.generateCacheKey(
				key.getIdentifier(),
				persister,
				session.getFactory(),
				session.getTenantIdentifier()
				);
		final Object cachedEntry = CacheHelper.fromSharedCache( session, ck, cache );
		if ( cachedEntry != null ) {
			CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure().destructure( cachedEntry, factory );
			return ( (ReferenceCacheEntryImpl) entry ).getReference();
		}
	}

	final Object object;
	if ( optionalObjectKey != null && key.equals( optionalObjectKey ) ) {
		//its the given optional object
		object = optionalObject;
	}
	else {
		// instantiate a new instance
		object = session.instantiate( instanceClass, key.getIdentifier() );
	}

	//need to hydrate it.

	// grab its state from the ResultSet and keep it in the Session
	// (but don't yet initialize the object itself)
	// note that we acquire LockMode.READ even if it was not requested
	LockMode acquiredLockMode = lockMode == LockMode.NONE ? LockMode.READ : lockMode;
	loadFromResultSet(
			rs,
			i,
			object,
			instanceClass,
			key,
			rowIdAlias,
			acquiredLockMode,
			persister,
			session
	);

	//materialize associations (and initialize the object) later
	hydratedObjects.add( object );

	return object;
}
 
Example 13
Source File: StatelessSessionImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
	public void refresh(String entityName, Object entity, LockMode lockMode) {
		final EntityPersister persister = this.getEntityPersister( entityName, entity );
		final Serializable id = persister.getIdentifier( entity, this );
		if ( LOG.isTraceEnabled() ) {
			LOG.tracev( "Refreshing transient {0}", MessageHelper.infoString( persister, id, this.getFactory() ) );
		}
		// TODO : can this ever happen???
//		EntityKey key = new EntityKey( id, persister, source.getEntityMode() );
//		if ( source.getPersistenceContext().getEntry( key ) != null ) {
//			throw new PersistentObjectException(
//					"attempted to refresh transient instance when persistent " +
//					"instance was already associated with the Session: " +
//					MessageHelper.infoString( persister, id, source.getFactory() )
//			);
//		}

		if ( persister.canWriteToCache() ) {
			final EntityDataAccess cacheAccess = persister.getCacheAccessStrategy();
			if ( cacheAccess != null ) {
				final Object ck = cacheAccess.generateCacheKey(
						id,
						persister,
						getFactory(),
						getTenantIdentifier()
				);
				cacheAccess.evict( ck );
			}
		}

		String previousFetchProfile = this.getLoadQueryInfluencers().getInternalFetchProfile();
		Object result = null;
		try {
			this.getLoadQueryInfluencers().setInternalFetchProfile( "refresh" );
			result = persister.load( id, entity, getNullSafeLockMode( lockMode ), this );
		}
		finally {
			this.getLoadQueryInfluencers().setInternalFetchProfile( previousFetchProfile );
		}
		UnresolvableObjectException.throwIfNull( result, id, persister.getEntityName() );
	}
 
Example 14
Source File: AbstractLockUpgradeEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Performs a pessimistic lock upgrade on a given entity, if needed.
 *
 * @param object The entity for which to upgrade the lock.
 * @param entry The entity's EntityEntry instance.
 * @param lockOptions contains the requested lock mode.
 * @param source The session which is the source of the event being processed.
 */
protected void upgradeLock(Object object, EntityEntry entry, LockOptions lockOptions, EventSource source) {

	LockMode requestedLockMode = lockOptions.getLockMode();
	if ( requestedLockMode.greaterThan( entry.getLockMode() ) ) {
		// The user requested a "greater" (i.e. more restrictive) form of
		// pessimistic lock

		if ( entry.getStatus() != Status.MANAGED ) {
			throw new ObjectDeletedException(
					"attempted to lock a deleted instance",
					entry.getId(),
					entry.getPersister().getEntityName()
			);
		}

		final EntityPersister persister = entry.getPersister();

		if ( log.isTraceEnabled() ) {
			log.tracev(
					"Locking {0} in mode: {1}",
					MessageHelper.infoString( persister, entry.getId(), source.getFactory() ),
					requestedLockMode
			);
		}

		final boolean cachingEnabled = persister.canWriteToCache();
		SoftLock lock = null;
		Object ck = null;
		try {
			if ( cachingEnabled ) {
				EntityDataAccess cache = persister.getCacheAccessStrategy();
				ck = cache.generateCacheKey( entry.getId(), persister, source.getFactory(), source.getTenantIdentifier() );
				lock = cache.lockItem( source, ck, entry.getVersion() );
			}

			if ( persister.isVersioned() && requestedLockMode == LockMode.FORCE  ) {
				// todo : should we check the current isolation mode explicitly?
				Object nextVersion = persister.forceVersionIncrement(
						entry.getId(), entry.getVersion(), source
				);
				entry.forceLocked( object, nextVersion );
			}
			else {
				persister.lock( entry.getId(), entry.getVersion(), object, lockOptions, source );
			}
			entry.setLockMode(requestedLockMode);
		}
		finally {
			// the database now holds a lock + the object is flushed from the cache,
			// so release the soft lock
			if ( cachingEnabled ) {
				persister.getCacheAccessStrategy().unlockItem( source, ck, lock );
			}
		}

	}
}
 
Example 15
Source File: EntityDeleteAction.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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.canWriteToCache() ) {
		final EntityDataAccess 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.canWriteToCache() ) {
		persister.getCacheAccessStrategy().remove( session, ck);
	}

	persistenceContext.getNaturalIdHelper().removeSharedNaturalIdCrossReference( persister, id, naturalIdValues );

	postDelete();

	if ( getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto ) {
		getSession().getFactory().getStatistics().deleteEntity( getPersister().getEntityName() );
	}
}
 
Example 16
Source File: EntityInsertAction.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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();

	// Don't need to lock the cache here, since if someone
	// else inserted the same pk first, the insert would fail

	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 EntityDataAccess 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().entityCachePut(
					StatsHelper.INSTANCE.getRootEntityRole( persister ),
					cache.getRegion().getName()
			);
		}
	}

	handleNaturalIdPostSaveNotifications( id );

	postInsert();

	if ( factory.getStatistics().isStatisticsEnabled() && !veto ) {
		factory.getStatistics().insertEntity( getPersister().getEntityName() );
	}

	markExecuted();
}