All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.stat.StatisticsImpl Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha1
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Middleware LLC.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 *
 */
package org.hibernate.stat;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.hibernate.cache.Region;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.util.ArrayHelper;

/**
 * @see org.hibernate.stat.Statistics 
 *  
 * @author Gavin King
 */
public class StatisticsImpl implements Statistics, StatisticsImplementor {
	
	//TODO: we should provide some way to get keys of collection of statistics to make it easier to retrieve from a GUI perspective
	
	private static final Logger log = LoggerFactory.getLogger(StatisticsImpl.class);

	private SessionFactoryImplementor sessionFactory;

	private boolean isStatisticsEnabled;
	private long startTime;
	private long sessionOpenCount;
	private long sessionCloseCount;
	private long flushCount;
	private long connectCount;
	
	private long prepareStatementCount;
	private long closeStatementCount;
	
	private long entityLoadCount;
	private long entityUpdateCount;
	private long entityInsertCount;
	private long entityDeleteCount;
	private long entityFetchCount;
	private long collectionLoadCount;
	private long collectionUpdateCount;
	private long collectionRemoveCount;
	private long collectionRecreateCount;
	private long collectionFetchCount;
	
	private long secondLevelCacheHitCount;
	private long secondLevelCacheMissCount;
	private long secondLevelCachePutCount;
	
	private long queryExecutionCount;
	private long queryExecutionMaxTime;
	private String queryExecutionMaxTimeQueryString;
	private long queryCacheHitCount;
	private long queryCacheMissCount;
	private long queryCachePutCount;
	
	private long commitedTransactionCount;
	private long transactionCount;
	
	private long optimisticFailureCount;
	
	/** second level cache statistics per region */
	private final Map secondLevelCacheStatistics = new HashMap();
	/** entity statistics per name */
	private final Map entityStatistics = new HashMap();
	/** collection statistics per name */
	private final Map collectionStatistics = new HashMap();
	/** entity statistics per query string (HQL or SQL) */
	private final Map queryStatistics = new HashMap();

	public StatisticsImpl() {
		clear();
	}

	public StatisticsImpl(SessionFactoryImplementor sessionFactory) {
		clear();
		this.sessionFactory = sessionFactory;
	}
	
	/**
	 * reset all statistics
	 */
	public synchronized void clear() {
		secondLevelCacheHitCount = 0;
		secondLevelCacheMissCount = 0;
		secondLevelCachePutCount = 0;
		
		sessionCloseCount = 0;
		sessionOpenCount = 0;
		flushCount = 0;
		connectCount = 0;
		
		prepareStatementCount = 0;
		closeStatementCount = 0;
		
		entityDeleteCount = 0;
		entityInsertCount = 0;
		entityUpdateCount = 0;
		entityLoadCount = 0;
		entityFetchCount = 0;
		
		collectionRemoveCount = 0;
		collectionUpdateCount = 0;
		collectionRecreateCount = 0;
		collectionLoadCount = 0;
		collectionFetchCount = 0;
		
		queryExecutionCount = 0;
		queryCacheHitCount = 0;
		queryExecutionMaxTime = 0;
		queryExecutionMaxTimeQueryString = null;
		queryCacheMissCount = 0;
		queryCachePutCount = 0;
		
		transactionCount = 0;
		commitedTransactionCount = 0;
		
		optimisticFailureCount = 0;
		
		secondLevelCacheStatistics.clear();
		entityStatistics.clear();
		collectionStatistics.clear();
		queryStatistics.clear();
		
		startTime = System.currentTimeMillis();
	}
	
	public synchronized void openSession() {
		sessionOpenCount++;
	}
	
	public synchronized void closeSession() {
		sessionCloseCount++;
	}
	
	public synchronized void flush() {
		flushCount++;
	}
	
	public synchronized void connect() {
		connectCount++;
	}
	
	public synchronized void loadEntity(String entityName) {
		entityLoadCount++;
		((EntityStatisticsImpl) getEntityStatistics(entityName)).loadCount++;
	}

	public synchronized void fetchEntity(String entityName) {
		entityFetchCount++;
		((EntityStatisticsImpl) getEntityStatistics(entityName)).fetchCount++;
	}

	/**
	 * find entity statistics per name
	 * 
	 * @param entityName entity name
	 * @return EntityStatistics object
	 */
	public synchronized EntityStatistics getEntityStatistics(String entityName) {
		EntityStatisticsImpl es = (EntityStatisticsImpl) entityStatistics.get(entityName);
		if (es == null) {
			es = new EntityStatisticsImpl(entityName);
			entityStatistics.put(entityName, es);
		}
		return es;
	}
	
	public synchronized void updateEntity(String entityName) {
		entityUpdateCount++;
		EntityStatisticsImpl es = (EntityStatisticsImpl) getEntityStatistics(entityName);
		es.updateCount++;
	}

	public synchronized void insertEntity(String entityName) {
		entityInsertCount++;
		EntityStatisticsImpl es = (EntityStatisticsImpl) getEntityStatistics(entityName);
		es.insertCount++;
	}

	public synchronized void deleteEntity(String entityName) {
		entityDeleteCount++;
		EntityStatisticsImpl es = (EntityStatisticsImpl) getEntityStatistics(entityName);
		es.deleteCount++;
	}

	/**
	 * Get collection statistics per role
	 * 
	 * @param role collection role
	 * @return CollectionStatistics
	 */
	public synchronized CollectionStatistics getCollectionStatistics(String role) {
		CollectionStatisticsImpl cs = (CollectionStatisticsImpl) collectionStatistics.get(role);
		if (cs==null) {
			cs = new CollectionStatisticsImpl(role);
			collectionStatistics.put(role, cs);
		}
		return cs;
	}
	
	public synchronized void loadCollection(String role) {
		collectionLoadCount++;
		((CollectionStatisticsImpl) getCollectionStatistics(role)).loadCount++;
	}

	public synchronized void fetchCollection(String role) {
		collectionFetchCount++;
		((CollectionStatisticsImpl) getCollectionStatistics(role)).fetchCount++;
	}

	public synchronized void updateCollection(String role) {
		collectionUpdateCount++;
		((CollectionStatisticsImpl) getCollectionStatistics(role)).updateCount++;
	}

	public synchronized void recreateCollection(String role) {
		collectionRecreateCount++;
		((CollectionStatisticsImpl) getCollectionStatistics(role)).recreateCount++;
	}

	public synchronized void removeCollection(String role) {
		collectionRemoveCount++;
		((CollectionStatisticsImpl) getCollectionStatistics(role)).removeCount++;
	}
	
	/**
	 * Second level cache statistics per region
	 * 
	 * @param regionName region name
	 * @return SecondLevelCacheStatistics
	 */
	public synchronized SecondLevelCacheStatistics getSecondLevelCacheStatistics(String regionName) {
		SecondLevelCacheStatisticsImpl slcs = (SecondLevelCacheStatisticsImpl) secondLevelCacheStatistics.get(regionName);
		if ( slcs == null ) {
			if ( sessionFactory == null ) {
				return null;
			}
			Region region = sessionFactory.getSecondLevelCacheRegion( regionName );
			if ( region == null ) {
				return null;
			}
			slcs = new SecondLevelCacheStatisticsImpl(region);
			secondLevelCacheStatistics.put( regionName, slcs );
		}
		return slcs;
	}

	public synchronized void secondLevelCachePut(String regionName) {
		secondLevelCachePutCount++;
		((SecondLevelCacheStatisticsImpl) getSecondLevelCacheStatistics(regionName)).putCount++;
	}

	public synchronized void secondLevelCacheHit(String regionName) {
		secondLevelCacheHitCount++;
		((SecondLevelCacheStatisticsImpl) getSecondLevelCacheStatistics(regionName)).hitCount++;
	}

	public synchronized void secondLevelCacheMiss(String regionName) {
		secondLevelCacheMissCount++;
		((SecondLevelCacheStatisticsImpl) getSecondLevelCacheStatistics(regionName)).missCount++;
	}

	public synchronized void queryExecuted(String hql, int rows, long time) {
		queryExecutionCount++;
		if (queryExecutionMaxTime-->
            
    
            


© 2015 - 2024 Weber Informatics LLC | Privacy Policy