-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate interceptor query
PostPosted: Tue Jun 09, 2009 4:15 am 
Newbie

Joined: Mon Jun 08, 2009 2:33 am
Posts: 5
I am trying to enable audit log in my application using interceptor.
I tried the following approach to get the hibernate current session.
Code:
public class ExtendedThreadLocalSessionContext extends
      ThreadLocalSessionContext {

   public ExtendedThreadLocalSessionContext(SessionFactoryImplementor factory) {
      super(factory);
      System.out.println("ExtendedThreadLocalSessionContext==>>");
      // TODO Auto-generated constructor stub
   }

   protected Session buildOrObtainSession()
{   
   AuditLogInterceptor interceptor = new AuditLogInterceptor();
      Session session = factory.openSession(null,interceptor);
      interceptor.setSession(session);
      interceptor.setUserId(0L);
      return session;   
}

}

with the above implementation of buildOrObtainSession(), we encounter the following error
Quote:
org.hibernate.HibernateException: isConnected is not valid without active transaction
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
$Proxy0.isConnected(Unknown Source)
com.gts.dao.hibernate.GenericHibernateDAO.getSession(GenericHibernateDAO.java:77)
com.gts.pms.booking.checklist.dao.ChecklistDAOHibernate.getCheckList(ChecklistDAOHibernate.java:24)
com.gts.pms.booking.checklist.service.ChecklistService.getCheckListGroupList(ChecklistService.java:29)
com.gts.pms.booking.checklist.delegate.ChecklistDelegate.getCheckListGroupList(ChecklistDelegate.java:23)
com.gts.pms.booking.BookingAction.execute(BookingAction.java:137)
org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
com.gts.filter.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:60)

when the following else Is executed.
Code:
protected Session getSession() {
      if (session == null) {
         session = HibernateUtil.getSessionFactory().getCurrentSession();
      } else if (!session.isConnected()) {
         session = HibernateUtil.getSessionFactory().getCurrentSession();
      }
      return session;
   }

Instead when we use the following implementation of buildOrObtainSession, there is no error but interceptor does not work.
Code:
protected Session buildOrObtainSession()
{
         
      return factory.openSession(
            null,
              isAutoFlushEnabled(),
           isAutoCloseEnabled(),
              getConnectionReleaseMode()
);


please suggest a solution to pass the interceptor to openSession() avoiding the abve error.
I suspect that not passin the "getConnectionReleaseMode()" is the problem


Top
 Profile  
 
 Post subject: Re: Hibernate interceptor query
PostPosted: Tue Jun 09, 2009 4:18 am 
Newbie

Joined: Mon Jun 08, 2009 2:33 am
Posts: 5
Also is the follwing configuration in Hibernate.cfg.xml correct ?
<property name="entityInterceptor">com.gts.persistence.AuditLogInterceptor</property>


Top
 Profile  
 
 Post subject: Re: Hibernate interceptor query
PostPosted: Fri Jun 12, 2009 4:58 am 
Newbie

Joined: Mon Jun 08, 2009 2:33 am
Posts: 5
Hi all

Could fix the above issue by having my own implementation of
opensession() method as follows

Code:
openSession(null, isAutoFlushEnabled(),isAutoCloseEnabled(),getConnectionReleaseMode(), interceptor);


with this i am able to pass the connectionReleaseMode while opening the session.


Top
 Profile  
 
 Post subject: Re: Hibernate interceptor query
PostPosted: Thu Jun 18, 2009 1:19 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
vamsee, I had the same type of requirement you're working on right now.

I submitted a patch under http://opensource.atlassian.com/project ... e/HHH-2960 which should allow what you want to do.

I then override the buildOrObtainSession like this
Code:
    protected Session buildOrObtainSession() {
        //return the factory with a new Hibernate Interceptor so that we can queue audit logging
        HibernateInterceptor interceptor = new HibernateInterceptor();
        Session session = factory.openSession(null, isAutoFlushEnabled(), isAutoCloseEnabled(), getConnectionReleaseMode(), interceptor);
        interceptor.setSession(session);
        return session;
    }

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject: Re: Hibernate interceptor query
PostPosted: Mon Jun 22, 2009 5:29 am 
Newbie

Joined: Mon Jun 08, 2009 2:33 am
Posts: 5
Thanks VampBoy, that was very helpful


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.