-->
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.  [ 8 posts ] 
Author Message
 Post subject: Failed to lazily initialize a collection - no session or ses
PostPosted: Sat May 28, 2005 5:09 am 
Newbie

Joined: Wed Apr 13, 2005 9:38 am
Posts: 7
This exception appears about once/hour, really annoying me.
And I am not sure if it will get much more frequestly with the improve of concurrency.

My analysis:
I open session for every thread, so the "no session" is impossible.
It seems that the session is closed by other thread before the owning thread make everything done.
But, I manipulate session with ThreadLocal and close session in filter,
so, the session shouldn't be closed by other thread!!!!


Hibernate version:
Hibernate 2.1.8

Mapping documents:

Code between sessionFactory.openSession() and session.close():

filter:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Log log = LogFactory.getLog(this.getClass());
try {
chain.doFilter(request, response);
} catch (Exception e) {
log.error(e);
} finally {
try {
HibernateUtil.closeSession();
} catch (HibernateException e1) {
log.error(e1);
}
}
}

helper class:

public class HibernateUtil {

private static SessionFactory sessionFactory;

static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.out.println("Initial SessionFactory creation failed.");
throw new ExceptionInInitializerError(ex);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null) {
s.close();
}
}
}

The BaseAction, all my other actions extend it:

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession(true);
Transaction tx = null;
Log log = LogFactory.getLog(this.getClass());
try {
Session ss = HibernateUtil.currentSession();
tx = ss.beginTransaction();

// process code here
// process code here
// process code here

tx.commit();
return mapping.findForward("suc");
} catch (HibernateException e) {
if (tx != null) {
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
return mapping.findForward("fail");
} catch (SQLException e) {
if (tx != null) {
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
return mapping.findForward("fail");
} catch (IOException e) {
if (tx != null) {
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
return mapping.findForward("fail");
} catch (Exception e) {
if (tx != null) {
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
return mapping.findForward("fail");
}
}


Full stack trace of any exception that occurs:
ApplicationDispatcher[] Servlet.service() for servlet jsp threw exception
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:209)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
at net.sf.hibernate.collection.Set.iterator(Set.java:130)
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:360)
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:238)
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:172)
at javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:262)
at org.apache.jsp.pages.viewUser_jsp._jspx_meth_c_forEach_11(viewUser_jsp.java:4650)
at org.apache.jsp.pages.viewUser_jsp._jspx_meth_c_if_9(viewUser_jsp.java:4614)
at org.apache.jsp.pages.viewUser_jsp._jspService(viewUser_jsp.java:442)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:704)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:474)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:409)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at com.dodoyo.filter.DodoyoFilter.doFilter(Unknown Source)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:300)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:374)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:743)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:675)
at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:866)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)

Name and version of the database you are using:
PostgreSQL 7.4.3
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: A filter usually gets hit multiple times
PostPosted: Sun May 29, 2005 3:22 am 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Hi,

I experienced this same problem. You have to realize that, contrary to what you might expect, your Filter gets hit every time for every JSP include or tile. So, using a servlet filter does not guarantee a single Hibernate session across the request.

So, your session is closed and opened multiple times. In case of uninitialized elements in objects you use across these multiple sessions you will run into unitialized exceptions and the like.

I see you're using Struts. The solution is to extend your request processor and configure it to be called from your struts config. Open and close the hibernate session inside the process method of your extended request processor and you will have a guaranteed single hibernate session for the duration of the request.

something like
****extended requestprocessor******

public void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
try{
HibernateUtil.openSession();
catch(HibernateException he){
logger.error(he);
throw new ServletException(he);
}
//reattach any httpsession objects that may contain uninitialized collections
try{
super.process(request,response);
catch(Exception e){
logger.error(e);
throw new ServletException(e);
}finally{
try{
HibernateUtil.closeSession();
}catch(HibernateException he){
logger.warn(he);
}
}


Cheers,

Marc


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 29, 2005 9:06 am 
Newbie

Joined: Wed Apr 13, 2005 9:38 am
Posts: 7
Thanks a lot :)
I will try it and give the result in several days


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 3:17 am 
Newbie

Joined: Wed Apr 13, 2005 9:38 am
Posts: 7
I tried this approach.
It seems that it doesn't work.


Top
 Profile  
 
 Post subject: What doesn't work
PostPosted: Tue May 31, 2005 3:23 am 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
What kind of problems do you experience?

Marc


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 3:37 am 
Newbie

Joined: Wed Apr 13, 2005 9:38 am
Posts: 7
I disabled the filter,
extended the RequestProcessor.
But the exception appears as before:(


Top
 Profile  
 
 Post subject: Post your code
PostPosted: Tue May 31, 2005 3:46 am 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
I cannot help you without more specific info. Did you reference your extended request processor from the struts-config.xml?


<controller>
<set-property property="processorClass" value="your.package.CustomRequestProcessor" />
</controller>


Marc


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 3:49 am 
Newbie

Joined: Wed Apr 13, 2005 9:38 am
Posts: 7
yes, so I did.
otherwise, there will be many connections leave unclosed.

Any way, Thank you very much.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.