-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate lazy initialisation in a WebWork web application
PostPosted: Sun Jun 13, 2004 2:10 am 
Newbie

Joined: Wed Feb 11, 2004 12:03 pm
Posts: 15
Hi, I'm currently having troubles while trying to lazy initialise a set from a bean when I try to read this set from mys jsp page with webwork tags.
Here is the code of the page :
Code:
<%@ taglib prefix="ww" uri="webwork" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<html><body>
   
      <ww:push value="dataset">
            ...
            <ww:iterator value="parameters">
            <a href="<ww:url value="'showParameter.action' + (#status.count-1)"/>">
            <b><ww:property value="name"/></br></b>
            </a>
            </ww:iterator>

      </ww:push>
</body>
</html>


Here is the dataset xml mapping file with the set Parameters

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="datas.DataSet" table="DATASETS">
        <id name="id" type="string" unsaved-value="null">
         <column name="dataset_filename" sql-type="char(32)" not-null="true" />
         <generator class="assigned" />
      </id>
...
        <set name="parameters" cascade="all" inverse="true" lazy="true">
            <key column="parameter_id"/>
            <one-to-many class="datas.Parameter"/>       
        </set>
    </class>
</hibernate-mapping>


and here is the code of my session filter that I initialize with each url access as in my web.xml it is specified
Quote:
<filter>
<filter-name>SessionManager</filter-name>
<filter-class>utils.SessionManager</filter-class>
</filter>
<filter-mapping>
<filter-name>SessionManager</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


Code:
package utils;

import java.io.*;
import javax.servlet.*;

import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.cfg.Configuration;

public class SessionManager implements Filter
{
    protected static ThreadLocal hibernateHolder = new ThreadLocal();
    protected static ThreadLocal txHolder = new ThreadLocal();
    protected static SessionFactory factory;
   
     public void init(FilterConfig filterConfig) throws ServletException
     {
        // Initialize hibernate
        try
        {
            factory = new Configuration().configure().buildSessionFactory();
        }
        catch (HibernateException ex) { throw new ServletException(ex); }
     }
     
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                   throws IOException, ServletException
    {
        if (hibernateHolder.get() != null)
            throw new IllegalStateException(
                "A session is already associated with this thread!  "
                + "Someone must have called getSession() outside of the context "
                + "of a servlet request.");
           
        try
        {
            chain.doFilter(request, response);
        }
        finally
        {
            Session sess = (Session)hibernateHolder.get();
            Transaction tx = (Transaction)txHolder.get();
            if (sess != null)
            {
                hibernateHolder.set(null);
               
                try
                {
                    if (tx != null) {
                        tx.commit();
                    }
                    sess.close();
                }
                catch (HibernateException ex) { throw new ServletException(ex); }
            }
        }
    }
   
    public static Session getSession() throws HibernateException
    {
       //System.out.println("Getting session\n");
       Session sess = (Session)hibernateHolder.get();
       //System.out.println("Getting transaction\n");
        Transaction tx = (Transaction)txHolder.get();
               
        if (sess == null)
        {
           //System.out.println("Session doesn't exist, creating\n");
            sess = factory.openSession();
            //System.out.println("Session created\n");
            txHolder.set(sess.beginTransaction());
            //System.out.println("Session started\n");
            hibernateHolder.set(sess);
            //System.out.println("Session allocated\n");
        }
        else {
              if (tx == null) {
              throw new IllegalStateException("Transaction was allready rolled back");
             }
        }
        //System.out.println("Session successfully retrieved\n");
        return sess;
    }
   
    public static void rollback() throws HibernateException {
        Transaction tx = (Transaction)txHolder.get();
        if (tx == null) {
            throw new IllegalStateException("Transaction was allready rolled back");
        }
       
        tx.rollback();
        txHolder.set(null);
    }
               
    public void destroy() {
        try {
            factory.close();
        }
        catch (HibernateException ex) { throw new RuntimeException(ex); }
    }       
}


finally here is the error log :

Code:
ERROR - Failed to lazily initialize a collection - no session or session was closed
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 com.opensymphony.webwork.util.MakeIterator.convert(MakeIterator.java:60)
   at com.opensymphony.webwork.views.jsp.IteratorTag.doStartTag(IteratorTag.java:140)
   at org.apache.jsp.jsp.showdataset_jsp._jspx_meth_ww_iterator_0(showdataset_jsp.java:316)
   at org.apache.jsp.jsp.showdataset_jsp._jspx_meth_ww_push_0(showdataset_jsp.java:177)
   at org.apache.jsp.jsp.showdataset_jsp._jspx_meth_ww_form_0(showdataset_jsp.java:114)
   at org.apache.jsp.jsp.showdataset_jsp._jspService(showdataset_jsp.java:69)
   at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
   at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:298)
   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:810)
   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:703)
   at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
   at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
   at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)
   at com.opensymphony.webwork.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:64)
   at com.opensymphony.webwork.dispatcher.WebWorkResultSupport.execute(WebWorkResultSupport.java:53)
   at com.opensymphony.xwork.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:274)
   at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:192)
   at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:37)
   at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:170)
   at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:37)
   at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:170)
   at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:37)
   at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:170)
   at com.opensymphony.xwork.DefaultActionProxy.execute(DefaultActionProxy.java:116)
   at com.opensymphony.webwork.dispatcher.ServletDispatcher.serviceAction(ServletDispatcher.java:182)
   at com.opensymphony.webwork.dispatcher.ServletDispatcher.service(ServletDispatcher.java:162)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
   at utils.SessionManager.doFilter(SessionManager.java:39)
   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:117)
   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.coyote.http11.Http11Processor.process(Http11Processor.java:793)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:702)
   at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:571)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:644)
   at java.lang.Thread.run(Unknown Source)
ERROR - Could not execute action
org.apache.jasper.JasperException: Failed to lazily initialize a collection - no session or session was closed
   at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:346)
   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:810)
   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:703)
   at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
   at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
   at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)
   at com.opensymphony.webwork.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:64)
   at com.opensymphony.webwork.dispatcher.WebWorkResultSupport.execute(WebWorkResultSupport.java:53)
   at com.opensymphony.xwork.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:274)
   at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:192)
   at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:37)
   at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:170)
   at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:37)
   at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:170)
   at com.opensymphony.xwork.interceptor.AroundInterceptor.intercept(AroundInterceptor.java:37)
   at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:170)
   at com.opensymphony.xwork.DefaultActionProxy.execute(DefaultActionProxy.java:116)
   at com.opensymphony.webwork.dispatcher.ServletDispatcher.serviceAction(ServletDispatcher.java:182)
   at com.opensymphony.webwork.dispatcher.ServletDispatcher.service(ServletDispatcher.java:162)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
   at utils.SessionManager.doFilter(SessionManager.java:39)
   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:117)
   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.coyote.http11.Http11Processor.process(Http11Processor.java:793)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:702)
   at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:571)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:644)
   at java.lang.Thread.run(Unknown Source)


Has someone an idea on how persist my session until webwork ask hibernate to lazy initialise the set please ? ^^;


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

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.