Hi Marcio, I use Hibernate with Struts and the way I ensure that the session does not get closed is by using a filter. This filter opens up and closes a session at the beginning and end of each request.
Then, if I need to use those objects again I associate them with the session and carry on. This is my filter in the web.xml file: <!-- These filters are for the hibernate sessions --> <filter> <filter-name>HibernateFilter</filter-name> <filter-class>com.camp.common.utilities.HibernateFilter</filter-class> </filter> This is the code for the filter above: package com.camp.common.utilities; import org.apache.commons.logging.*; import javax.servlet.Filter; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.FilterConfig; import javax.servlet.FilterChain; import java.io.IOException; /** * A servlet filter that opens and closes a Hibernate Session for each request. * <p> * This filter guarantees a sane state, committing any pending database * transaction once all other filters (and servlets) have executed. It also * guarantees that the Hibernate <tt>Session</tt> of the current thread will * be closed before the response is send to the client. * <p> * Use this filter for the <b>session-per-request</b> pattern and if you are * using <i>Detached Objects</i>. * * @see HibernateUtil * @author Christian Bauer <[EMAIL PROTECTED]> */ public class HibernateFilter implements Filter { private static Log log = LogFactory.getLog(HibernateFilter.class); public void init(FilterConfig filterConfig) throws ServletException { log.debug("Servlet filter init, now opening/closing a Session for each request."); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { log.debug("Inside HibernateFilter.doFilter"); // We don't start the database transaction here, but when first needed chain.doFilter(request, response); log.debug("After chain.doFilter"); //Commit any pending database transaction. HibernateUtil.commitTransaction(); log.debug("After commitTransaction"); } catch (Exception e) { log.debug("An Exception was caught"); e.printStackTrace(); } finally { log.debug("Closing the Sesion"); // No matter what happens, close the Session. HibernateUtil.closeSession(); } } public void destroy() {} } As the comments suggest you could probably find an example of this at the hibernate website. Cheers Tom -----Original Message----- From: Marcio Ghiraldelli [mailto:[EMAIL PROTECTED] Sent: Friday, February 10, 2006 9:23 AM To: user@struts.apache.org Subject: MVC + ORM I´m using Struts 1.2.8 with Hibernate 3.0.5 and I´m doubt with some concept designs for ORM with MVC: A Hibernate session get's instantiated in my DAO constructor (Model). Later on, the JSP (View) catches the Hibernate persisted objects populated in the request or session by the Controller and shows the data. Question is: If I close Hibernate session in the Model, before the View, as by default the objects are lazy-instantiated, that is, the data would be retrieved from DB when it´s accessed, the JSP complains that session is closed. Besides, closing the session after the View I would be breaking the MVC pattern. I'm trying to retrieve Blob objects not sucessfully, due the problem above... the only way would be disable lazy-instantiation?? --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]