Java Code Examples for org.hibernate.SessionFactory#getStatistics()
The following examples show how to use
org.hibernate.SessionFactory#getStatistics() .
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: TestStatistics.java From HibernateTips with MIT License | 6 votes |
@Test public void statisticsAPI() { log.info("... statisticsAPI ..."); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); List<Author> authors = em.createQuery("SELECT a FROM Author a", Author.class).getResultList(); for (Author a : authors) { log.info(a.getFirstName() + " " + a.getLastName() + " wrote " + a.getBooks().size()); } SessionFactory sessionFactory = emf.unwrap(SessionFactory.class); Statistics stats = sessionFactory.getStatistics(); long queryCount = stats.getQueryExecutionCount(); long collectionFetchCount = stats.getCollectionFetchCount(); log.info("QueryCount: "+queryCount); log.info("CollectionFetchCount: "+collectionFetchCount); em.getTransaction().commit(); em.close(); }
Example 2
Source File: PaddingInQueryPlanCacheTest.java From high-performance-java-persistence with Apache License 2.0 | 6 votes |
@Test public void testInQueryCachePlan() { SessionFactory sessionFactory = entityManagerFactory().unwrap(SessionFactory.class); Statistics statistics = sessionFactory.getStatistics(); statistics.clear(); doInJPA(entityManager -> { for (int i = 2; i < 16; i++) { getPostByIds( entityManager, IntStream.range(1, i).boxed().toArray(Integer[]::new) ); } assertEquals(6L, statistics.getQueryPlanCacheMissCount()); for (String query : statistics.getQueries()) { LOGGER.info("Executed query: {}", query); } }); }
Example 3
Source File: DefaultInQueryPlanCacheTest.java From high-performance-java-persistence with Apache License 2.0 | 6 votes |
@Test public void testCriteriaAPI() { SessionFactory sessionFactory = entityManagerFactory().unwrap(SessionFactory.class); Statistics statistics = sessionFactory.getStatistics(); statistics.clear(); doInJPA(entityManager -> { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Post> criteria = builder.createQuery(Post.class); Root<Post> fromPost = criteria.from(Post.class); criteria.where(builder.in(fromPost.get("id")).value(Arrays.asList(1, 2, 3))); List<Post> posts = entityManager.createQuery(criteria).getResultList(); }); for (String query : statistics.getQueries()) { LOGGER.info("Executed query: {}", query); } }
Example 4
Source File: DefaultInQueryPlanCacheTest.java From high-performance-java-persistence with Apache License 2.0 | 6 votes |
@Test public void testInQueryCachePlan() { SessionFactory sessionFactory = entityManagerFactory().unwrap(SessionFactory.class); Statistics statistics = sessionFactory.getStatistics(); statistics.clear(); doInJPA(entityManager -> { for (int i = 1; i < 16; i++) { getPostByIds( entityManager, IntStream.range(1, i + 1).boxed().toArray(Integer[]::new) ); } }); assertEquals(16L, statistics.getQueryPlanCacheMissCount()); for (String query : statistics.getQueries()) { LOGGER.info("Executed query: {}", query); } }
Example 5
Source File: DefaultInQueryPlanCacheTest.java From high-performance-java-persistence with Apache License 2.0 | 6 votes |
@Test public void testJPQL() { SessionFactory sessionFactory = entityManagerFactory().unwrap(SessionFactory.class); Statistics statistics = sessionFactory.getStatistics(); statistics.clear(); doInJPA(entityManager -> { List<Post> posts = entityManager.createQuery( "select p " + "from Post p " + "where p.id in :ids", Post.class) .setParameter("ids", Arrays.asList(1, 2, 3)) .getResultList(); }); for (String query : statistics.getQueries()) { LOGGER.info("Executed query: {}", query); } }
Example 6
Source File: HibernateStatsReporter.java From keycloak with Apache License 2.0 | 5 votes |
@Override public void run(KeycloakSession session) { SessionFactory sessionFactory = ((SessionFactoryImpl) emf); Statistics stats = sessionFactory.getStatistics(); logStats(stats); stats.clear(); // For now, clear stats after each iteration }
Example 7
Source File: HibernateStatisticsCollector.java From client_java with Apache License 2.0 | 5 votes |
private void addMetricsForQuery(PerQuerySamples samples, ValueProviderPerQuery provider) { for (Entry<String, SessionFactory> entry : sessionFactories.entrySet()) { SessionFactory sessionFactory = entry.getValue(); Statistics stats = sessionFactory.getStatistics(); String unitName = entry.getKey(); for (String query : stats.getQueries()) { samples.addMetric(Arrays.asList(unitName, query), provider.getValue(stats, query)); } } }
Example 8
Source File: CacheHandlerTest.java From pnc with Apache License 2.0 | 5 votes |
@Test public void testMappedSecondLevelCacheStats() { Session session = (Session) em_1.getDelegate(); SessionFactory sessionFactory = session.getSessionFactory(); Statistics statistics = sessionFactory.getStatistics(); // Initialize sample build configurations, these cannot be done by DBUnit because of the Hibernate Envers // Auditing insertExampleBuildConfigurations(em_1, basicRepositoryConfiguration); SortedMap<String, Map<String, HibernateMetric>> secondLevelCacheStatMap = getSecondLevelCacheRegionsStats( statistics); logger.debug("All second level cache stats: {}", secondLevelCacheStatMap); String[] mappedEntities = { // REGION_STATS_PREFIX + "org.jboss.pnc.model.Artifact", REGION_STATS_PREFIX + "org.jboss.pnc.model.BuildConfigSetRecord", REGION_STATS_PREFIX + "org.jboss.pnc.model.BuildConfiguration", REGION_STATS_PREFIX + "org.jboss.pnc.model.BuildConfigurationSet", REGION_STATS_PREFIX + "org.jboss.pnc.model.BuildEnvironment", REGION_STATS_PREFIX + "org.jboss.pnc.model.BuildRecord", REGION_STATS_PREFIX + "org.jboss.pnc.model.BuildRecordPushResult", REGION_STATS_PREFIX + "org.jboss.pnc.model.Product", REGION_STATS_PREFIX + "org.jboss.pnc.model.ProductMilestone", REGION_STATS_PREFIX + "org.jboss.pnc.model.ProductMilestoneRelease", REGION_STATS_PREFIX + "org.jboss.pnc.model.ProductRelease", REGION_STATS_PREFIX + "org.jboss.pnc.model.ProductVersion", REGION_STATS_PREFIX + "org.jboss.pnc.model.Project", REGION_STATS_PREFIX + "org.jboss.pnc.model.RepositoryConfiguration", REGION_STATS_PREFIX + "org.jboss.pnc.model.TargetRepository", REGION_STATS_PREFIX + "org.jboss.pnc.model.User" }; Set<String> mappedEntitiesSet = new HashSet<String>(Arrays.asList(mappedEntities)); assertTrue(secondLevelCacheStatMap.keySet().containsAll(mappedEntitiesSet)); }
Example 9
Source File: CacheHandlerTest.java From pnc with Apache License 2.0 | 5 votes |
@Test public void testMappedEntitiesStats() { Session session = (Session) em_1.getDelegate(); SessionFactory sessionFactory = session.getSessionFactory(); Statistics statistics = sessionFactory.getStatistics(); // Initialize sample build configurations, these cannot be done by DBUnit because of the Hibernate Envers // Auditing insertExampleBuildConfigurations(em_1, basicRepositoryConfiguration); SortedMap<String, Map<String, HibernateMetric>> entitiesStatMap = getSecondLevelCacheEntitiesStats(statistics); logger.debug("All entities stats: {}", entitiesStatMap); String[] mappedEntities = { // ENTITY_STATS_PREFIX + "org.jboss.pnc.model.Artifact", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.BuildConfigSetRecord", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.BuildConfiguration", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.BuildConfigurationSet", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.BuildConfiguration_AUD", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.BuildEnvironment", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.BuildRecord", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.BuildRecordPushResult", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.Product", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.ProductMilestone", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.ProductMilestoneRelease", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.ProductRelease", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.ProductVersion", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.Project", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.RepositoryConfiguration", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.TargetRepository", ENTITY_STATS_PREFIX + "org.jboss.pnc.model.User", ENTITY_STATS_PREFIX + "build_configuration_parameters_AUD" }; Set<String> mappedEntitiesSet = new HashSet<String>(Arrays.asList(mappedEntities)); assertTrue(entitiesStatMap.keySet().containsAll(mappedEntitiesSet)); }
Example 10
Source File: StatisticsTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
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 11
Source File: HibernateEHCacheMain.java From journaldev with MIT License | 5 votes |
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 12
Source File: StatsTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
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 13
Source File: CacheHandlerRepositoryImpl.java From pnc with Apache License 2.0 | 4 votes |
@Override public SortedMap<String, Map<String, HibernateMetric>> getSecondLevelCacheEntitiesStats() { SessionFactory sessionFactory = ((Session) entityManager.getDelegate()).getSessionFactory(); Statistics statistics = sessionFactory.getStatistics(); return HibernateStatsUtils.getSecondLevelCacheEntitiesStats(statistics); }
Example 14
Source File: CacheHandlerRepositoryImpl.java From pnc with Apache License 2.0 | 4 votes |
@Override public SortedMap<String, Map<String, HibernateMetric>> getSecondLevelCacheRegionsStats() { SessionFactory sessionFactory = ((Session) entityManager.getDelegate()).getSessionFactory(); Statistics statistics = sessionFactory.getStatistics(); return HibernateStatsUtils.getSecondLevelCacheRegionsStats(statistics); }
Example 15
Source File: CacheHandlerRepositoryImpl.java From pnc with Apache License 2.0 | 4 votes |
@Override public SortedMap<String, Map<String, HibernateMetric>> getSecondLevelCacheCollectionsStats() { SessionFactory sessionFactory = ((Session) entityManager.getDelegate()).getSessionFactory(); Statistics statistics = sessionFactory.getStatistics(); return HibernateStatsUtils.getSecondLevelCacheCollectionsStats(statistics); }
Example 16
Source File: CacheHandlerRepositoryImpl.java From pnc with Apache License 2.0 | 4 votes |
@Override public SortedMap<String, HibernateMetric> getGenericStats() { SessionFactory sessionFactory = ((Session) entityManager.getDelegate()).getSessionFactory(); Statistics statistics = sessionFactory.getStatistics(); return HibernateStatsUtils.getGenericStats(statistics); }
Example 17
Source File: ApiInfoHandlers.java From devicehive-java-server with Apache License 2.0 | 4 votes |
private String getCacheStats() { SessionFactory sessionFactory = entityManagerFactory.getNativeEntityManagerFactory().unwrap(SessionFactory.class); Statistics statistics = sessionFactory.getStatistics(); return statistics.toString(); }
Example 18
Source File: CacheHandlerTest.java From pnc with Apache License 2.0 | 4 votes |
@Test public void testMappedCollectionsStats() { Session session = (Session) em_1.getDelegate(); SessionFactory sessionFactory = session.getSessionFactory(); Statistics statistics = sessionFactory.getStatistics(); // Initialize sample build configurations, these cannot be done by DBUnit because of the Hibernate Envers // Auditing insertExampleBuildConfigurations(em_1, basicRepositoryConfiguration); SortedMap<String, Map<String, HibernateMetric>> collectionStatMap = getSecondLevelCacheCollectionsStats( statistics); logger.debug("All collection stats: {}", collectionStatMap); String[] mappedCollections = { COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildConfigurationSet.buildConfigurations", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildConfigSetRecord.buildRecords", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.ProductVersion.buildConfigurations", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildRecord.attributes", // COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.TargetRepository.artifacts", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildConfiguration.genericParameters", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.ProductMilestone.performedBuilds", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildConfiguration.dependants", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildRecord.dependencies", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.RepositoryConfiguration.buildConfigurations", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildConfiguration.dependencies", // COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.Artifact.distributedInProductMilestones", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.ProductVersion.attributes", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.User.buildRecords", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildEnvironment.attributes", // COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildRecord.builtArtifacts", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildConfigSetRecord.attributes", // COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.ProductMilestone.distributedArtifacts", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.Project.buildConfigurations", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.ProductVersion.buildConfigurationSets", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildRecord.buildRecordPushResults", // COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.Artifact.dependantBuildRecords", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.Product.productVersions", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.ProductVersion.productMilestones", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildConfiguration.buildConfigurationSets", COLLECTION_STATS_PREFIX + "org.jboss.pnc.model.BuildConfigurationSet.buildConfigSetRecords" }; Set<String> mappedCollectionsSet = new HashSet<String>(Arrays.asList(mappedCollections)); assertTrue(collectionStatMap.keySet().containsAll(mappedCollectionsSet)); }
Example 19
Source File: ApiInfoResourceImpl.java From devicehive-java-server with Apache License 2.0 | 4 votes |
private String getCacheStats() { SessionFactory sessionFactory = entityManagerFactory.getNativeEntityManagerFactory().unwrap(SessionFactory.class); Statistics statistics = sessionFactory.getStatistics(); return statistics.toString(); }
Example 20
Source File: HibernateMetrics.java From micrometer with Apache License 2.0 | 2 votes |
/** * Create a {@code HibernateMetrics}. * @param sessionFactory session factory to use * @param sessionFactoryName session factory name as a tag value * @param tags additional tags */ public HibernateMetrics(SessionFactory sessionFactory, String sessionFactoryName, Iterable<Tag> tags) { this.tags = Tags.concat(tags, SESSION_FACTORY_TAG_NAME, sessionFactoryName); Statistics statistics = sessionFactory.getStatistics(); this.statistics = statistics.isStatisticsEnabled() ? statistics : null; }