use of org.hibernate.stat.Statistics in project microservices by pwillhan.
the class SecondLevel method cacheModes.
@Test
public void cacheModes() throws Exception {
CacheTestData testData = storeTestData();
Long USER_ID = testData.users.getFirstId();
Long ITEM_ID = testData.items.getFirstId();
UserTransaction tx = TM.getUserTransaction();
try {
{
tx.begin();
EntityManager em = JPA.createEntityManager();
Statistics stats = JPA.getEntityManagerFactory().unwrap(SessionFactory.class).getStatistics();
SecondLevelCacheStatistics itemCacheStats = stats.getSecondLevelCacheStatistics(Item.class.getName());
// Bypass the cache when retrieving an entity instance by identifier
{
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
// Hit the database
Item item = em.find(Item.class, ITEM_ID, properties);
assertEquals(itemCacheStats.getHitCount(), 0);
}
// Bypass the cache when storing an entity instance
assertEquals(itemCacheStats.getElementCountInMemory(), 3);
em.setProperty("javax.persistence.cache.storeMode", CacheStoreMode.BYPASS);
Item item = new Item(// ...
"Some Item", CalendarUtil.TOMORROW.getTime(), em.find(User.class, USER_ID));
// Not stored in the cache
em.persist(item);
em.flush();
// Unchanged
assertEquals(itemCacheStats.getElementCountInMemory(), 3);
tx.commit();
em.close();
}
} finally {
TM.rollback();
}
}
use of org.hibernate.stat.Statistics in project openmrs-core by openmrs.
the class HibernateContextDAO method showUsageStatistics.
/**
* Convenience method to print out the hibernate cache usage stats to the log
*/
private void showUsageStatistics() {
if (sessionFactory.getStatistics().isStatisticsEnabled()) {
log.debug("Getting query statistics: ");
Statistics stats = sessionFactory.getStatistics();
for (String query : stats.getQueries()) {
log.info("QUERY: " + query);
QueryStatistics qstats = stats.getQueryStatistics(query);
log.info("Cache Hit Count : " + qstats.getCacheHitCount());
log.info("Cache Miss Count: " + qstats.getCacheMissCount());
log.info("Cache Put Count : " + qstats.getCachePutCount());
log.info("Execution Count : " + qstats.getExecutionCount());
log.info("Average time : " + qstats.getExecutionAvgTime());
log.info("Row Count : " + qstats.getExecutionRowCount());
}
}
}
use of org.hibernate.stat.Statistics in project wildfly by wildfly.
the class SFSB2LC method sameSessionCheck.
/**
* Checking entity 2LC in one EntityManager session
* TODO: rewrite to separate transaction used to createEmployee, and either load entities outside tx or
* start new tx for loading entities. Maybe simplest to have calling code, handle creating the Employees first.
* TODO: need conclusion to discussion about whether createEmployee should cause 2lc PutCount to be incremented,
* as no data is loaded into the cache.
*/
public String sameSessionCheck(String CACHE_REGION_NAME) {
EntityManager em = emf.createEntityManager();
Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
stats.clear();
CacheRegionStatistics emp2LCStats = stats.getDomainDataRegionStatistics(CACHE_REGION_NAME + "Employee");
try {
// add new entities and check if they are put in 2LC
createEmployee(em, "Peter", "Ostrava", 2);
createEmployee(em, "Tom", "Brno", 3);
assertEquals("There are 2 puts in the 2LC" + generateEntityCacheStats(emp2LCStats), 2, emp2LCStats.getPutCount());
// loading all Employee entities should put in 2LC all Employee
List<?> empList = getAllEmployeesQuery(em);
assertEquals("There are 2 entities.", empList.size(), 2);
assertEquals("There are 2 entities in the 2LC" + generateEntityCacheStats(emp2LCStats), 2, emp2LCStats.getElementCountInMemory());
// clear session
em.clear();
// entity should be loaded from 2L cache, we'are expecting hit in 2L cache
Employee emp = getEmployee(em, 2);
assertNotNull("Employee returned", emp);
assertEquals("Expected 1 hit in cache" + generateEntityCacheStats(emp2LCStats), 1, emp2LCStats.getHitCount());
} catch (AssertionError e) {
return e.getMessage();
} finally {
em.close();
}
return "OK";
}
use of org.hibernate.stat.Statistics in project wildfly by wildfly.
the class SFSB2LC method disabled2LCCheck.
/**
* Check if disabling 2LC works as expected
*/
public String disabled2LCCheck() {
EntityManager em = emfNo2LC.createEntityManager();
Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
stats.clear();
try {
// check if entities are NOT cached in 2LC
String[] names = stats.getSecondLevelCacheRegionNames();
assertEquals("There aren't any 2LC regions.", 0, names.length);
createEmployee(em, "Martin", "Prague 132", 1);
assertEquals("There aren't any puts in the 2LC.", 0, stats.getSecondLevelCachePutCount());
// check if queries are NOT cached in 2LC
Employee emp = getEmployeeQuery(em, 1);
assertNotNull("Employee returned", emp);
assertEquals("There aren't any query puts in the 2LC.", 0, stats.getQueryCachePutCount());
// cleanup
em.remove(emp);
} catch (AssertionError e) {
return e.getMessage();
} finally {
em.close();
}
return "OK";
}
use of org.hibernate.stat.Statistics in project OpenClinica by OpenClinica.
the class RuleSetRuleDao method findByRuleSetStudyIdAndStatusAvail.
/**
* Use this method carefully as we force an eager fetch. It is also annotated with
* Transactional so it can be called from Quartz threads.
* @param studyId
* @return List of RuleSetRuleBeans
*/
@SuppressWarnings("unchecked")
@Transactional
public ArrayList<RuleSetRuleBean> findByRuleSetStudyIdAndStatusAvail(Integer studyId) {
String query = "from " + getDomainClassName() + " ruleSetRule where ruleSetRule.ruleSetBean.studyId = :studyId and status = :status ";
org.hibernate.Query q = getCurrentSession().createQuery(query);
q.setInteger("studyId", studyId);
q.setParameter("status", org.akaza.openclinica.domain.Status.AVAILABLE);
q.setCacheable(true);
q.setCacheRegion(getDomainClassName());
// JN: enabling statistics for hibernate queries etc... to monitor the performance
Statistics stats = getSessionFactory().getStatistics();
logger.info("EntityRuleSet" + stats.getEntityInsertCount());
logger.info(stats.getQueryExecutionMaxTimeQueryString());
logger.info("hit count" + stats.getSecondLevelCacheHitCount());
stats.logSummary();
ArrayList<RuleSetRuleBean> ruleSetRules = (ArrayList<RuleSetRuleBean>) q.list();
// Forcing eager fetch of actions & their properties
for (RuleSetRuleBean ruleSetRuleBean : ruleSetRules) {
for (RuleActionBean action : ruleSetRuleBean.getActions()) {
if (action instanceof RandomizeActionBean) {
((RandomizeActionBean) action).getProperties().size();
}
if (action instanceof InsertActionBean) {
((InsertActionBean) action).getProperties().size();
}
if (action instanceof ShowActionBean) {
((ShowActionBean) action).getProperties().size();
}
if (action instanceof HideActionBean) {
((HideActionBean) action).getProperties().size();
}
if (action instanceof EventActionBean) {
((EventActionBean) action).getProperties().size();
}
}
}
return ruleSetRules;
}
Aggregations