0

I have a java EE , spring application that uses Hibernate 3. I want to calculate the execution time of queries, so I am trying to intercept hibernation session and query methods to calculate the execution time.

I have added spring-aop and aspectjweaver jars to my project This is where I am excuting hibernate session and query methods

@Repository
public abstract class PlatformHibernateDaoSupport {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    public SessionFactory getSessionFactory(){
        return this.sessionFactory;
    }
    
    public Session getSession(){
        return this.sessionFactory.getCurrentSession();
    }

public Serializable hibernateSave(Object object){
        return getSession().save(object);
    }

public Serializable saveEntity(String entityName, Object object){
        return getSession().save(entityName, object);
    }
    
    
    public void hibernateSaveOrUpdate(Object object){
        getSession().saveOrUpdate(object);
    }

    
    @SuppressWarnings("unchecked")
    public <T> List<T> find(String hql, Class<T> clazz){    
        Query query = getSession().createQuery(hql);        
        return (List<T>)query.list();
    }
    
}

And this is the aspect

public class HibernateQueryExecutionTimeListener {


    public Object find(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long elapsedTime = System.currentTimeMillis() - startTime;
        String method = joinPoint.getSignature().getName();
        System.out.println("MethodName" + method + ", executed in " + elapsedTime + " ms: " );
        return result;
    }


    public Object save(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long elapsedTime = System.currentTimeMillis() - startTime;
        String method = joinPoint.getSignature().getName();
        System.out.println("MethodName" + method + ", executed in " + elapsedTime + " ms: " );
        return result;
    }


    public Object saveOrUpdate(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long elapsedTime = System.currentTimeMillis() - startTime;
        String method = joinPoint.getSignature().getName();
        System.out.println("MethodName" + method + ", executed in " + elapsedTime + " ms: " );
        return result;
    }


    public Object load(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long elapsedTime = System.currentTimeMillis() - startTime;
        String method = joinPoint.getSignature().getName();
        System.out.println("MethodName" + method + ", executed in " + elapsedTime + " ms: " );
        return result;
    }

}

And this are the configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- Hibernate SessionFactory -->
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="myDataSource"/>
        </property>

<property name="mappingResources">
            <list>
                        <value>com/javaEE/deliver/api/model/Customer.hbm.xml</value>
                        
        </list>
        </property>

<aop:aspectj-autoproxy />


     <bean id="hibernateQueryExecutionTimeListener" class="com.isa.javaEE.deliver.core.HibernateQueryExecutionTimeListener" />

      <aop:config>
        <aop:aspect id="hibernateaspect" ref="hibernateQueryExecutionTimeListener" >
           <aop:pointcut id="queryListAround"   expression="execution(* org.hibernate.Query.list(..))" />
           <aop:around method="find" pointcut-ref="queryListAround" />

           <aop:pointcut id="saveAround"   expression="execution(* org.hibernate.Session.save(..))" />
           <aop:around method="save" pointcut-ref="saveAround" />

           <aop:pointcut id="saveOrUpdateAround"   expression="execution(* org.hibernate.Session.saveOrUpdate(..))" />
           <aop:around method="saveOrUpdate" pointcut-ref="saveOrUpdateAround" />

           <aop:pointcut id="loadAround"   expression="execution(* org.hibernate.Session.load(..))" />
           <aop:around method="load" pointcut-ref="loadAround" />

        </aop:aspect>
      </aop:config>

    </bean>
</beans>

And my problem is that this advices are never invoked at all, neither the logs are printing.So, I am not sure where to go next. How can I get a hold of Hibernate's Session object from my advice such that I can enable Hibernate filters? Thank you in advance!

I tired changing the aop configurations and all, but I couldn't figure it out. I don't know if I even need to configure that anyways.

0 Answers0