Skip to content

Instantly share code, notes, and snippets.

@bmakarand2009
Created February 6, 2014 07:46
Show Gist options
  • Save bmakarand2009/8839905 to your computer and use it in GitHub Desktop.
Save bmakarand2009/8839905 to your computer and use it in GitHub Desktop.
Get the Hibernate Statistics
package com.webforefront.aop;
import org.hibernate.stat.Statistics;
import org.hibernate.SessionFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import javax.persistence.EntityManagerFactory;
import org.hibernate.ejb.HibernateEntityManagerFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@Aspect
public class CacheHibernateInterceptor {
private Log log = LogFactory.getLog(DAOInterceptor.class);
@Autowired
private EntityManagerFactory entityManagerFactory;
@Around("execution(* com.webforefront.jpa.service..*.*(..))")
public Object log(ProceedingJoinPoint pjp) throws Throwable {
HibernateEntityManagerFactory hbmanagerfactory = (HibernateEntityManagerFactory) entityManagerFactory;
SessionFactory sessionFactory = hbmanagerfactory.getSessionFactory();
Statistics statistics = sessionFactory.getStatistics();
String str = pjp.getTarget().toString();
statistics.setStatisticsEnabled(true);
log.info(str.substring(str.lastIndexOf(".")+1, str.lastIndexOf("@")) + " - " + pjp.getSignature().getName() + ": (Before call) " + statistics);
Object result = pjp.proceed();
log.info(str.substring(str.lastIndexOf(".")+1, str.lastIndexOf("@")) + " - " + pjp.getSignature().getName() + ": (After call) " + statistics);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment