Java Code Examples for org.hibernate.stat.Statistics#setStatisticsEnabled()

The following examples show how to use org.hibernate.stat.Statistics#setStatisticsEnabled() . 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: HibernateEHCacheMain.java    From journaldev with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	
	System.out.println("Temp Dir:"+System.getProperty("java.io.tmpdir"));
	
	//Initialize Sessions
	SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	Statistics stats = sessionFactory.getStatistics();
	System.out.println("Stats enabled="+stats.isStatisticsEnabled());
	stats.setStatisticsEnabled(true);
	System.out.println("Stats enabled="+stats.isStatisticsEnabled());
	
	Session session = sessionFactory.openSession();
	Session otherSession = sessionFactory.openSession();
	Transaction transaction = session.beginTransaction();
	Transaction otherTransaction = otherSession.beginTransaction();
	
	printStats(stats, 0);
	
	Employee emp = (Employee) session.load(Employee.class, 1L);
	printData(emp, stats, 1);
	
	emp = (Employee) session.load(Employee.class, 1L);
	printData(emp, stats, 2);
	
	//clear first level cache, so that second level cache is used
	session.evict(emp);
	emp = (Employee) session.load(Employee.class, 1L);
	printData(emp, stats, 3);
	
	emp = (Employee) session.load(Employee.class, 3L);
	printData(emp, stats, 4);
	
	emp = (Employee) otherSession.load(Employee.class, 1L);
	printData(emp, stats, 5);
	
	//Release resources
	transaction.commit();
	otherTransaction.commit();
	sessionFactory.close();
}
 
Example 2
Source File: StatisticsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSessionStats() throws Exception {
	
	SessionFactory sf = getSessions();
	Statistics stats = sf.getStatistics();
	boolean isStats = stats.isStatisticsEnabled();
	stats.clear();
	stats.setStatisticsEnabled(true);
	Session s = sf.openSession();
	assertEquals( 1, stats.getSessionOpenCount() );
	s.close();
	assertEquals( 1, stats.getSessionCloseCount() );
	s = sf.openSession();
	Transaction tx = s.beginTransaction();
	A a = new A();
	a.setName("mya");
	s.save(a);
	a.setName("b");
	tx.commit();
	s.close();
	assertEquals( 1, stats.getFlushCount() );
	s = sf.openSession();
	tx = s.beginTransaction();
	String hql = "from " + A.class.getName();
	Query q = s.createQuery(hql);
	q.list();
	tx.commit();
	s.close();
	assertEquals(1, stats.getQueryExecutionCount() );
	assertEquals(1, stats.getQueryStatistics(hql).getExecutionCount() );
	
	stats.setStatisticsEnabled(isStats);
}
 
Example 3
Source File: SessionStatsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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);

}
 
Example 4
Source File: RelationshipsRepositoryTest.java    From spring-data-jpa-datatables with Apache License 2.0 5 votes vote down vote up
@Test
public void checkFetchJoin() {
    Statistics statistics = sessionFactory.getStatistics();
    statistics.setStatisticsEnabled(true);

    DataTablesOutput<A> output = getOutput(input);

    assertThat(output.getRecordsFiltered()).isEqualTo(3);
    assertThat(statistics.getPrepareStatementCount()).isEqualTo(2);
    assertThat(statistics.getEntityLoadCount()).isEqualTo(3 /* A */ + 3 /* C */);
}
 
Example 5
Source File: StatsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testCollectionFetchVsLoad() throws Exception {
	Statistics stats = getSessions().getStatistics();
	stats.clear();

	Session s = openSession();
	Transaction tx = s.beginTransaction();
	Continent europe = fillDb(s);
	tx.commit();
	s.clear();

	tx = s.beginTransaction();
	assertEquals(0, stats.getCollectionLoadCount() );
	assertEquals(0,  stats.getCollectionFetchCount() );
	Continent europe2 = (Continent) s.get( Continent.class, europe.getId() );
	assertEquals("Lazy true: no collection should be loaded", 0, stats.getCollectionLoadCount() );
	assertEquals( 0, stats.getCollectionFetchCount() );
	europe2.getCountries().size();
	assertEquals( 1, stats.getCollectionLoadCount() );
	assertEquals("Explicit fetch of the collection state", 1, stats.getCollectionFetchCount() );
	tx.commit();
	s.close();

	s = openSession();
	tx = s.beginTransaction();
	stats.clear();
	europe = fillDb(s);
	tx.commit();
	s.clear();
	tx = s.beginTransaction();
	assertEquals( 0, stats.getCollectionLoadCount() );
	assertEquals( 0, stats.getCollectionFetchCount() );
	europe2 = (Continent) s.createQuery(
			"from " + Continent.class.getName() + " a join fetch a.countries where a.id = " + europe.getId()
		).uniqueResult();
	assertEquals( 1, stats.getCollectionLoadCount() );
	assertEquals( "collection should be loaded in the same query as its parent", 0, stats.getCollectionFetchCount() );
	tx.commit();
	s.close();

	Collection coll = getCfg().getCollectionMapping(Continent.class.getName() + ".countries");
	coll.setFetchMode(FetchMode.JOIN);
	coll.setLazy(false);
	SessionFactory sf = getCfg().buildSessionFactory();
	stats = sf.getStatistics();
	stats.clear();
	stats.setStatisticsEnabled(true);
	s = sf.openSession();
	tx = s.beginTransaction();
	europe = fillDb(s);
	tx.commit();
	s.clear();
	tx = s.beginTransaction();
	assertEquals( 0, stats.getCollectionLoadCount() );
	assertEquals( 0, stats.getCollectionFetchCount() );
	europe2 = (Continent) s.get( Continent.class, europe.getId() );
	assertEquals( 1, stats.getCollectionLoadCount() );
	assertEquals( "Should do direct load, not indirect second load when lazy false and JOIN", 0, stats.getCollectionFetchCount() );
	tx.commit();
	s.close();
	sf.close();

	coll = getCfg().getCollectionMapping(Continent.class.getName() + ".countries");
	coll.setFetchMode(FetchMode.SELECT);
	coll.setLazy(false);
	sf = getCfg().buildSessionFactory();
	stats = sf.getStatistics();
	stats.clear();
	stats.setStatisticsEnabled(true);
	s = sf.openSession();
	tx = s.beginTransaction();
	europe = fillDb(s);
	tx.commit();
	s.clear();
	tx = s.beginTransaction();
	assertEquals( 0, stats.getCollectionLoadCount() );
	assertEquals( 0, stats.getCollectionFetchCount() );
	europe2 = (Continent) s.get( Continent.class, europe.getId() );
	assertEquals( 1, stats.getCollectionLoadCount() );
	assertEquals( "Should do explicit collection load, not part of the first one", 1, stats.getCollectionFetchCount() );
	Iterator countries = europe2.getCountries().iterator();
	while ( countries.hasNext() ) {
		s.delete( countries.next() );
	}
	cleanDb( s );
	tx.commit();
	s.close();
}
 
Example 6
Source File: JobsMemoryMonitorRunner.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
public JobsMemoryMonitorRunner(Statistics statistics, SchedulerState schedulerState) {
    statistics.setStatisticsEnabled(true);
    this.stats = statistics;
    this.schedulerState = schedulerState;
}