Example usage for org.hibernate SessionFactory getStatistics

List of usage examples for org.hibernate SessionFactory getStatistics

Introduction

In this page you can find the example usage for org.hibernate SessionFactory getStatistics.

Prototype

Statistics getStatistics();

Source Link

Document

Retrieve the statistics fopr this factory.

Usage

From source file:com.francetelecom.clara.cloud.commons.hibernate.HibernateStatisticsFactoryBean.java

License:Apache License

@Override
public Statistics getObject() throws Exception {
    SessionFactory sessionFactory = ((HibernateEntityManagerFactory) entityManagerFactory).getSessionFactory();
    return sessionFactory.getStatistics();
}

From source file:com.googlecode.hibernate.audit.listener.AuditSessionFactoryObserver.java

License:Open Source License

public void sessionFactoryCreated(SessionFactory sessionfactory) {
    initializeAuditMetatdata(sessionfactory);

    Statistics statistics = sessionfactory.getStatistics();

    CONFIGURATION_MAP.put(sessionfactory, auditConfiguration);
}

From source file:com.hazelcast.hibernate.HibernateTestSupport.java

License:Open Source License

protected SessionFactory createSessionFactory(Properties props, IHazelcastInstanceLoader customInstanceLoader) {
    Configuration conf = new Configuration();
    URL xml = HibernateTestSupport.class.getClassLoader().getResource("test-hibernate.cfg.xml");
    props.put(CacheEnvironment.EXPLICIT_VERSION_CHECK, "true");
    if (customInstanceLoader != null) {
        props.put("com.hazelcast.hibernate.instance.loader", customInstanceLoader);
        customInstanceLoader.configure(props);
    } else {//from   w ww .  j a va 2 s  .  com
        props.remove("com.hazelcast.hibernate.instance.loader");
    }
    conf.configure(xml);
    conf.setCacheConcurrencyStrategy(DummyEntity.class.getName(), getCacheStrategy());
    conf.setCacheConcurrencyStrategy(DummyProperty.class.getName(), getCacheStrategy());
    conf.setCollectionCacheConcurrencyStrategy(DummyEntity.class.getName() + ".properties", getCacheStrategy());
    conf.addProperties(props);
    final SessionFactory sf = conf.buildSessionFactory();
    sf.getStatistics().setStatisticsEnabled(true);
    return sf;
}

From source file:es.emergya.bbdd.dao.RecursoHome.java

License:Open Source License

@Transactional(readOnly = true, rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
public Recurso getbyDispositivoServer(Integer disp) {
    long now = System.currentTimeMillis();
    Recurso res = null;/*  w w  w.jav a  2s  .c o  m*/
    Session currentSession = getSession();
    SessionFactory sessionFactory = (SessionFactory) MyBeanFactory.getBean("sessionFactory");
    Criteria criteria = currentSession.createCriteria(Recurso.class).add(Restrictions.eq("dispositivo", disp))
            .setCacheable(true);
    res = (Recurso) criteria.uniqueResult();
    System.out.print("time run " + disp + ": " + (System.currentTimeMillis() - now));
    System.out.println(", cacheStatsRecurso = " + sessionFactory.getStatistics()
            .getSecondLevelCacheStatistics("es.emergya.bbdd.bean.Recurso").toString());
    System.out.println("QueryStats = queryCacheHits= " + sessionFactory.getStatistics().getQueryCacheHitCount()
            + ", queryCacheMiss=" + sessionFactory.getStatistics().getQueryCacheMissCount() + ", queryCachePut="
            + sessionFactory.getStatistics().getQueryCachePutCount());

    return res;
}

From source file:gr.interamerican.bo2.impl.open.hibernate.HibernateBo2Utils.java

License:Open Source License

/**
 * Logs {@link SessionFactory} statistics.
 * //w  w w. java 2s .com
 * @param sessionFactory 
 */
@SuppressWarnings("nls")
public static void logSessionFactoryStatistics(SessionFactory sessionFactory) {
    Statistics statistics = sessionFactory.getStatistics();
    Object[] stats = new Object[] { statistics.getCollectionFetchCount(), statistics.getCollectionLoadCount(),
            statistics.getEntityFetchCount(), statistics.getEntityLoadCount(), statistics.getFlushCount(),
            statistics.getOptimisticFailureCount(), statistics.getQueryExecutionMaxTime(),
            statistics.getQueryExecutionMaxTimeQueryString(), statistics.getSessionOpenCount(),
            statistics.getSecondLevelCacheHitCount(), statistics.getSecondLevelCacheMissCount() };
    StringBuilder sb = new StringBuilder();
    for (Object o : stats) {
        String s = (o == null) ? "null" : o.toString();
        sb.append(s + StringConstants.COMMA + StringConstants.SPACE);
    }
    sb.setLength(sb.length() - 2);
    Debug.debug(logger, "Factory statistics: [ " + sb.toString() + " ]");
}

From source file:gr.interamerican.bo2.impl.open.hibernate.HibernateConfigurations.java

License:Open Source License

/**
 * Creates a SessionFactory.//from  w w w  . j ava  2s . c  om
 * 
 * @param pathToCfg
 *        Path to the hibernate configuration file.
 * @param dbSchema
 *        Db schema.
 * @param sessionInterceptor
 *        Hibernate session interceptor.
 * @param hibernateMappingsPath
 *        Path to file that lists files indexing hbm files this session factory
 *        should be configured with
 * 
 * @return Returns the session factory.
 * 
 * @throws InitializationException
 *         If the creation of the SessionFactory fails.
 */
@SuppressWarnings("nls")
static SessionFactory createSessionFactory(String pathToCfg, String dbSchema, String sessionInterceptor,
        String hibernateMappingsPath) throws InitializationException {
    try {
        Configuration conf = new Configuration();

        Interceptor interceptor = getInterceptor(sessionInterceptor);
        if (interceptor != null) {
            conf.setInterceptor(interceptor);
        }

        conf.setProperty(SCHEMA_PROPERTY, dbSchema);

        List<String> hbms = getHibernateMappingsIfAvailable(hibernateMappingsPath);
        for (String entityMapping : hbms) {
            LOGGER.debug("Adding " + entityMapping + " to the session factory configuration.");
            conf.addResource(entityMapping);
        }

        conf.configure(pathToCfg);

        conf.getEntityTuplizerFactory().registerDefaultTuplizerClass(EntityMode.POJO,
                Bo2PojoEntityTuplizer.class);
        SessionFactory sessionFactory = conf.buildSessionFactory();
        ((SessionFactoryImpl) sessionFactory).registerEntityNameResolver(Bo2EntityNameResolver.INSTANCE,
                EntityMode.POJO);
        sessionFactory.getStatistics().setStatisticsEnabled(true);
        return sessionFactory;
    } catch (HibernateException e) {
        throw new InitializationException(e);
    }
}

From source file:io.micrometer.core.instrument.binder.jpa.HibernateMetrics.java

License:Apache License

/**
 * Get the {@code Statistics} object from the underlying {@code SessionFactory}. If it isn't hibernate that is
 * used return {@code null}./*from www .j a  v  a 2  s. com*/
 *
 * @param emf an {@code EntityManagerFactory}
 * @return the {@code Statistics} from the underlying {@code SessionFactory} or {@code null}.
 */
@Nullable
private Statistics getStatistics(EntityManagerFactory emf) {
    try {
        SessionFactory sf = emf.unwrap(SessionFactory.class);
        return sf.getStatistics();
    } catch (PersistenceException pe) {
        return null;
    }
}

From source file:io.micrometer.core.instrument.binder.jpa.HibernateMetricsTest.java

License:Apache License

private static EntityManagerFactory createEntityManagerFactoryMock(final boolean statsEnabled) {
    EntityManagerFactory emf = Mockito.mock(EntityManagerFactory.class);
    SessionFactory sf = Mockito.mock(SessionFactory.class);
    Statistics stats = Mockito.mock(Statistics.class, invocation -> {
        if (invocation.getMethod().getName().equals("isStatisticsEnabled")) {
            return statsEnabled;
        }//  w ww . j  ava  2  s. c  o  m
        return 42L;
    });
    when(emf.unwrap(SessionFactory.class)).thenReturn(sf);
    when(sf.getStatistics()).thenReturn(stats);
    return emf;
}

From source file:kiwi.action.debug.StatisticsAction.java

License:Open Source License

public void dumpHibernateStatistics() {
    SessionFactory sf = hibernateSession.getSessionFactory();
    Statistics s = sf.getStatistics();

    s.logSummary();
}

From source file:net.eisele.hibernatestatistics.jpa.JpaTest.java

License:Apache License

private static void registerHibernateMBeans(EntityManagerFactory emf) {
    SessionFactory sessionFactory = emf.unwrap(SessionFactory.class);

    try {/*from   w ww .  ja v  a 2 s.  c  o m*/
        MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName on = new ObjectName("Hibernate:type=statistics,application=hibernatestatistics");

        StatisticsService mBean = new DelegatingStatisticsService(sessionFactory.getStatistics());
        mBean.setStatisticsEnabled(true); // alternative is to enable it in persistence.xml
        mbeanServer.registerMBean(mBean, on);

    } catch (MalformedObjectNameException ex) {
        logger.error("", ex);
    } catch (InstanceAlreadyExistsException ex) {
        logger.error("", ex);
    } catch (MBeanRegistrationException ex) {
        logger.error("", ex);
    } catch (NotCompliantMBeanException ex) {
        logger.error("", ex);
    }

}