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