Java Code Examples for org.hibernate.persister.entity.EntityPersister#hasCache()

The following examples show how to use org.hibernate.persister.entity.EntityPersister#hasCache() . 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: BatchFetchQueue.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private boolean isCached(
		EntityKey entityKey,
		EntityPersister persister,
		EntityMode entityMode) {
	if ( persister.hasCache() ) {
		CacheKey key = new CacheKey(
				entityKey.getIdentifier(),
				persister.getIdentifierType(),
				entityKey.getEntityName(),
				entityMode,
				context.getSession().getFactory()
		);
		return persister.getCache().getCache().get( key ) != null;
	}
	return false;
}
 
Example 2
Source File: EntityUpdateAction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void afterTransactionCompletion(boolean success) throws CacheException {
	EntityPersister persister = getPersister();
	if ( persister.hasCache() ) {
		
		final CacheKey ck = new CacheKey( 
				getId(), 
				persister.getIdentifierType(), 
				persister.getRootEntityName(), 
				getSession().getEntityMode(), 
				getSession().getFactory() 
			);
		
		if ( success && cacheEntry!=null /*!persister.isCacheInvalidationRequired()*/ ) {
			boolean put = persister.getCache().afterUpdate(ck, cacheEntry, nextVersion, lock );
			
			if ( put && getSession().getFactory().getStatistics().isStatisticsEnabled() ) {
				getSession().getFactory().getStatisticsImplementor()
						.secondLevelCachePut( getPersister().getCache().getRegionName() );
			}
		}
		else {
			persister.getCache().release(ck, lock );
		}
	}
	postCommitUpdate();
}
 
Example 3
Source File: DefaultLoadEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 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.
 *
 * @return The loaded entity
 * @throws HibernateException
 */
protected Object lockAndLoad(
	final LoadEvent event, 
	final EntityPersister persister,
	final EntityKey keyToLoad, 
	final LoadEventListener.LoadType options,
	final SessionImplementor source) 
throws HibernateException {
	
	CacheConcurrencyStrategy.SoftLock lock = null;
	final CacheKey ck;
	if ( persister.hasCache() ) {
		ck = new CacheKey( 
				event.getEntityId(), 
				persister.getIdentifierType(), 
				persister.getRootEntityName(), 
				source.getEntityMode(), 
				source.getFactory() 
			);
		lock = persister.getCache().lock(ck, null );
	}
	else {
		ck = null;
	}

	Object entity;
	try {
		entity = load(event, persister, keyToLoad, options);
	}
	finally {
		if ( persister.hasCache() ) {
			persister.getCache().release(ck, lock );
		}
	}

	Object proxy = event.getSession().getPersistenceContext()
			.proxyFor( persister, keyToLoad, entity );
	
	return proxy;
}
 
Example 4
Source File: SessionFactoryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void evictEntity(String entityName, Serializable id) throws HibernateException {
	EntityPersister p = getEntityPersister(entityName);
	if ( p.hasCache() ) {
		if ( log.isDebugEnabled() ) {
			log.debug( "evicting second-level cache: " + MessageHelper.infoString(p, id, this) );
		}
		CacheKey cacheKey = new CacheKey( id, p.getIdentifierType(), p.getRootEntityName(), EntityMode.POJO, this );
		p.getCache().remove( cacheKey );
	}
}
 
Example 5
Source File: SessionFactoryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void evictEntity(String entityName) throws HibernateException {
	EntityPersister p = getEntityPersister(entityName);
	if ( p.hasCache() ) {
		if ( log.isDebugEnabled() ) {
			log.debug( "evicting second-level cache: " + p.getEntityName() );
		}
		p.getCache().clear();
	}
}
 
Example 6
Source File: SessionFactoryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void evict(Class persistentClass, Serializable id) throws HibernateException {
	EntityPersister p = getEntityPersister( persistentClass.getName() );
	if ( p.hasCache() ) {
		if ( log.isDebugEnabled() ) {
			log.debug( "evicting second-level cache: " + MessageHelper.infoString(p, id, this) );
		}
		CacheKey cacheKey = new CacheKey( id, p.getIdentifierType(), p.getRootEntityName(), EntityMode.POJO, this );
		p.getCache().remove( cacheKey );
	}
}
 
Example 7
Source File: SessionFactoryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void evict(Class persistentClass) throws HibernateException {
	EntityPersister p = getEntityPersister( persistentClass.getName() );
	if ( p.hasCache() ) {
		if ( log.isDebugEnabled() ) {
			log.debug( "evicting second-level cache: " + p.getEntityName() );
		}
		p.getCache().clear();
	}
}
 
Example 8
Source File: BulkOperationCleanupAction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/** Create an action that will evict collection and entity regions based on queryspaces (table names).
 *  TODO: cache the autodetected information and pass it in instead.
 **/
public BulkOperationCleanupAction(SessionImplementor session, Set querySpaces) {
	this.session = session;

	Set tmpSpaces = new HashSet(querySpaces);
	SessionFactoryImplementor factory = session.getFactory();
	Iterator iterator = factory.getAllClassMetadata().entrySet().iterator();
	while ( iterator.hasNext() ) {
		Map.Entry entry = (Map.Entry) iterator.next();
		String entityName = (String) entry.getKey();
		EntityPersister persister = factory.getEntityPersister( entityName );
		Serializable[] entitySpaces = persister.getQuerySpaces();

		if (affectedEntity( querySpaces, entitySpaces )) {
			if ( persister.hasCache() ) {
				affectedEntityNames.add( persister.getEntityName() );
			}
			Set roles = session.getFactory().getCollectionRolesByEntityParticipant( persister.getEntityName() );
			if ( roles != null ) {
				affectedCollectionRoles.addAll( roles );
			}
			for ( int y = 0; y < entitySpaces.length; y++ ) {
				tmpSpaces.add( entitySpaces[y] );
			}
		}

	}
	this.spaces = (Serializable[]) tmpSpaces.toArray( new Serializable[tmpSpaces.size()] );		
}
 
Example 9
Source File: AbstractLockUpgradeEventListener.java    From cacheonix-core 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 requestedLockMode The lock mode being requested for locking.
 * @param source The session which is the source of the event being processed.
 * @throws HibernateException
 */
protected void upgradeLock(Object object, EntityEntry entry, LockMode requestedLockMode, SessionImplementor source)
throws HibernateException {

	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.trace(
					"locking " +
					MessageHelper.infoString( persister, entry.getId(), source.getFactory() ) +
					" in mode: " +
					requestedLockMode
			);

		final CacheConcurrencyStrategy.SoftLock lock;
		final CacheKey ck;
		if ( persister.hasCache() ) {
			ck = new CacheKey( 
					entry.getId(), 
					persister.getIdentifierType(), 
					persister.getRootEntityName(), 
					source.getEntityMode(), 
					source.getFactory() 
			);
			lock = persister.getCache().lock( ck, entry.getVersion() );
		}
		else {
			ck = null;
			lock = null;
		}
		
		try {
			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, requestedLockMode, source );
			}
			entry.setLockMode(requestedLockMode);
		}
		finally {
			// the database now holds a lock + the object is flushed from the cache,
			// so release the soft lock
			if ( persister.hasCache() ) {
				persister.getCache().release(ck, lock );
			}
		}

	}
}
 
Example 10
Source File: DefaultLoadEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Attempts to load the entity from the second-level cache.
 *
 * @param event The load event
 * @param persister The persister for the entity being requested for load
 * @param options The load options.
 * @return The entity from the second-level cache, or null.
 * @throws HibernateException
 */
protected Object loadFromSecondLevelCache(
		final LoadEvent event,
		final EntityPersister persister,
		final LoadEventListener.LoadType options) throws HibernateException {
	
	final SessionImplementor source = event.getSession();
	
	final boolean useCache = persister.hasCache() && 
		source.getCacheMode().isGetEnabled() && 
		event.getLockMode().lessThan(LockMode.READ);
	
	if (useCache) {
		
		final SessionFactoryImplementor factory = source.getFactory();
		
		final CacheKey ck = new CacheKey( 
				event.getEntityId(), 
				persister.getIdentifierType(), 
				persister.getRootEntityName(),
				source.getEntityMode(), 
				source.getFactory()
			);
		Object ce = persister.getCache()
			.get( ck, source.getTimestamp() );
		
		if ( factory.getStatistics().isStatisticsEnabled() ) {
			if (ce==null) {
				factory.getStatisticsImplementor().secondLevelCacheMiss( 
					persister.getCache().getRegionName() 
				);
			}
			else {
				factory.getStatisticsImplementor().secondLevelCacheHit( 
					persister.getCache().getRegionName() 
				);
			}
		}

		if ( ce != null ) {

			CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure()
					.destructure(ce, factory);
		
			// Entity was found in second-level cache...
			return assembleCacheEntry(
					entry,
					event.getEntityId(),
					persister,
					event
				);
		}
	}
	
	return null;
}
 
Example 11
Source File: StatelessSessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void refresh(String entityName, Object entity, LockMode lockMode) {
		final EntityPersister persister = this.getEntityPersister( entityName, entity );
		final Serializable id = persister.getIdentifier( entity, getEntityMode() );
		if ( log.isTraceEnabled() ) {
			log.trace(
					"refreshing transient " +
					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.hasCache() ) {
			final CacheKey ck = new CacheKey(
					id,
			        persister.getIdentifierType(),
			        persister.getRootEntityName(),
			        this.getEntityMode(),
			        this.getFactory()
			);
			persister.getCache().remove(ck);
		}

		String previousFetchProfile = this.getFetchProfile();
		Object result = null;
		try {
			this.setFetchProfile( "refresh" );
			result = persister.load( id, entity, lockMode, this );
		}
		finally {
			this.setFetchProfile( previousFetchProfile );
		}
		UnresolvableObjectException.throwIfNull( result, id, persister.getEntityName() );
	}
 
Example 12
Source File: EntityDeleteAction.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void execute() throws HibernateException {
	Serializable id = getId();
	EntityPersister persister = getPersister();
	SessionImplementor session = getSession();
	Object instance = getInstance();

	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, session.getEntityMode() );
	}

	final CacheKey ck;
	if ( persister.hasCache() ) {
		ck = new CacheKey( 
				id, 
				persister.getIdentifierType(), 
				persister.getRootEntityName(), 
				session.getEntityMode(), 
				session.getFactory() 
			);
		lock = persister.getCache().lock(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();
	EntityEntry entry = persistenceContext.removeEntry( instance );
	if ( entry == null ) {
		throw new AssertionFailure( "possible nonthreadsafe access to session" );
	}
	entry.postDelete();

	EntityKey key = new EntityKey( entry.getId(), entry.getPersister(), session.getEntityMode() );
	persistenceContext.removeEntity(key);
	persistenceContext.removeProxy(key);
	
	if ( persister.hasCache() ) persister.getCache().evict(ck);

	postDelete();

	if ( getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto ) {
		getSession().getFactory().getStatisticsImplementor()
				.deleteEntity( getPersister().getEntityName() );
	}
}
 
Example 13
Source File: EntityInsertAction.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private boolean isCachePutEnabled(EntityPersister persister, SessionImplementor session) {
	return persister.hasCache() && 
			!persister.isCacheInvalidationRequired() && 
			session.getCacheMode().isPutEnabled();
}