下面列出了怎么用org.hibernate.stat.SessionStatistics的API类实例代码及写法,或者点击链接到github查看源代码。
public void testSessionStatistics() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Statistics stats = getSessions().getStatistics();
stats.clear();
boolean isStats = stats.isStatisticsEnabled();
stats.setStatisticsEnabled(true);
Continent europe = fillDb(s);
tx.commit();
s.clear();
tx = s.beginTransaction();
SessionStatistics sessionStats = s.getStatistics();
assertEquals( 0, sessionStats.getEntityKeys().size() );
assertEquals( 0, sessionStats.getEntityCount() );
assertEquals( 0, sessionStats.getCollectionKeys().size() );
assertEquals( 0, sessionStats.getCollectionCount() );
europe = (Continent) s.get( Continent.class, europe.getId() );
Hibernate.initialize( europe.getCountries() );
Hibernate.initialize( europe.getCountries().iterator().next() );
assertEquals( 2, sessionStats.getEntityKeys().size() );
assertEquals( 2, sessionStats.getEntityCount() );
assertEquals( 1, sessionStats.getCollectionKeys().size() );
assertEquals( 1, sessionStats.getCollectionCount() );
tx.commit();
s.close();
stats.setStatisticsEnabled( isStats);
}
private void printSessionStats(SessionStatistics sessionStatistics) {
logger.debug("--- FIRST LEVEL STATS ---");
logger.debug("Session stats collectionCount: {}", sessionStatistics.getCollectionCount());
logger.debug("Session stats collectionKeys: {}", sessionStatistics.getCollectionKeys());
logger.debug("Session stats entityCount: {}", sessionStatistics.getEntityCount());
logger.debug("Session stats entityKeys: {}", sessionStatistics.getEntityKeys());
logger.debug("--- --- --- --- --- ---");
}
@Override
public SessionStatistics getStatistics() {
return delegate.getStatistics();
}
@Override
public SessionStatistics getStatistics() {
checkTransactionSynchStatus();
return new SessionStatisticsImpl( this );
}
public SessionStatistics getStatistics() {
checkTransactionSynchStatus();
return new SessionStatisticsImpl(this);
}
/**
* Get the statistics for this session.
*
* @return The session statistics being collected for this session
*/
SessionStatistics getStatistics();
/**
* Get the statistics for this session.
*/
public SessionStatistics getStatistics();