Java Code Examples for org.hibernate.stat.spi.StatisticsImplementor#isStatisticsEnabled()

The following examples show how to use org.hibernate.stat.spi.StatisticsImplementor#isStatisticsEnabled() . 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: CachingReactiveLoader.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
default CompletionStage<List<Object>> doReactiveList(
		final String sql, final String queryIdentifier,
		final SessionImplementor session,
		final QueryParameters queryParameters,
		final ResultTransformer forcedResultTransformer)
		throws HibernateException {

	final StatisticsImplementor statistics = session.getSessionFactory().getStatistics();
	final boolean stats = statistics.isStatisticsEnabled();
	final long startTime = stats ? System.nanoTime() : 0;

	return doReactiveQueryAndInitializeNonLazyCollections( sql, session, queryParameters, true, forcedResultTransformer )
			.handle( (list, err) -> {
				CompletionStages.logSqlException( err, () -> "could not execute query", sql );

				if ( err ==null && stats ) {
					final long endTime = System.nanoTime();
					final long milliseconds = TimeUnit.MILLISECONDS.convert( endTime - startTime, TimeUnit.NANOSECONDS );
					statistics.queryExecuted( queryIdentifier, list.size(), milliseconds );
				}

				return CompletionStages.returnOrRethrow(err, list );
			} );
}
 
Example 2
Source File: DefaultReactiveLoadEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Performs the process of loading an entity from the configured
 * underlying datasource.
 *
 * @param event The load event
 * @param persister The persister for the entity being requested for load
 *
 * @return The object loaded from the datasource, or null if not found.
 */
protected CompletionStage<Object> loadFromDatasource(
		final LoadEvent event,
		final EntityPersister persister) {

	CompletionStage<Object> entity =
			( (ReactiveEntityPersister) persister).reactiveLoad(
					event.getEntityId(),
					event.getInstanceToLoad(),
					event.getLockOptions(),
					event.getSession()
			);

	final StatisticsImplementor statistics = event.getSession().getFactory().getStatistics();
	if ( event.isAssociationFetch() && statistics.isStatisticsEnabled() ) {
		statistics.fetchEntity( event.getEntityClassName() );
	}

	return entity;
}
 
Example 3
Source File: DefaultReactiveFlushEntityEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Object[] getDatabaseSnapshot(SessionImplementor session, EntityPersister persister, Serializable id) {
	final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
	if ( persister.isSelectBeforeUpdateRequired() ) {
		Object[] snapshot = persistenceContext
				.getDatabaseSnapshot( id, persister );
		if ( snapshot == null ) {
			//do we even really need this? the update will fail anyway....
			final StatisticsImplementor statistics = session.getFactory().getStatistics();
			if ( statistics.isStatisticsEnabled() ) {
				statistics
						.optimisticFailure( persister.getEntityName() );
			}
			throw new StaleObjectStateException( persister.getEntityName(), id );
		}
		return snapshot;
	}
	// TODO: optimize away this lookup for entities w/o unsaved-value="undefined"
	final EntityKey entityKey = session.generateEntityKey( id, persister );
	return persistenceContext.getCachedDatabaseSnapshot( entityKey );
}
 
Example 4
Source File: DefaultReactiveInitializeCollectionEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Try to initialize a collection from the cache
 *
 * @param id The id of the collection to initialize
 * @param persister The collection persister
 * @param collection The collection to initialize
 * @param source The originating session
 *
 * @return true if we were able to initialize the collection from the cache;
 *         false otherwise.
 */
private boolean initializeCollectionFromCache(
		Serializable id,
		CollectionPersister persister,
		PersistentCollection collection,
		SessionImplementor source) {

	if ( source.getLoadQueryInfluencers().hasEnabledFilters() && persister.isAffectedByEnabledFilters( source ) ) {
		LOG.trace( "Disregarding cached version (if any) of collection due to enabled filters" );
		return false;
	}

	final boolean useCache = persister.hasCache() && source.getCacheMode().isGetEnabled();

	if ( !useCache ) {
		return false;
	}

	final SessionFactoryImplementor factory = source.getFactory();
	final CollectionDataAccess cacheAccessStrategy = persister.getCacheAccessStrategy();
	final Object ck = cacheAccessStrategy.generateCacheKey( id, persister, factory, source.getTenantIdentifier() );
	final Object ce = CacheHelper.fromSharedCache( source, ck, cacheAccessStrategy );

	final StatisticsImplementor statistics = factory.getStatistics();
	if ( statistics.isStatisticsEnabled() ) {
		if ( ce == null ) {
			statistics.collectionCacheMiss( persister.getNavigableRole(), cacheAccessStrategy.getRegion().getName() );
		}
		else {
			statistics.collectionCacheHit( persister.getNavigableRole(), cacheAccessStrategy.getRegion().getName() );
		}
	}

	if ( ce == null ) {
		return false;
	}

	CollectionCacheEntry cacheEntry = (CollectionCacheEntry) persister.getCacheEntryStructure().destructure( ce, factory );

	final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
	cacheEntry.assemble( collection, persister, persistenceContext.getCollectionOwner( id, persister ) );
	persistenceContext.getCollectionEntry( collection ).postInitialize( collection );
	return true;
}