Hey Jon!

I also think that this is a great idea :)

What i dont like about your approach is that it always creates a session.

>    String vSessionId =
> ((HttpServletRequest)request).getSession(true).getId();
>    Session vSession = (Session)request.getAttribute(vSessionId);

Why did you do it this way? Is it not ok to use a string constant as
attribute name?

I think my approach will be to open the session in the controller, and close
it only in the filter, if there is an open session.

regards
 chris



----- Original Message -----
From: "Jon Lipsky" <[EMAIL PROTECTED]>
To: "Christoph Sturm" <[EMAIL PROTECTED]>; "Brad Clow"
<[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Tuesday, August 27, 2002 11:46 AM
Subject: Re: [Hibernate-devel] mvc & lazy loading


> Hi All,
>
> I use a Filter (a new addition in the 2.3 servlet spec) to open and close
my
> Hibernate sessions.  By doing it this way it doesn't matter if I am using
> Velocity or JSP or something else to access Hibernate.  As far as the
"view"
> is concerned the Hibernate session just exists, and only the Filter has to
> worry about opening and closing it.
>
> I was looking at the examples inclued with Hibernate and I was thinking
that
> maybe an example should be added of using a Filter since it's a good way
to
> cleanly seperate the creation and closing of the sessions for a web
> application.
>
> Jon...
>
> PS - Here is a code snippet to get you started if you want to do it this
> way:
>
> package example;
>
> import cirrus.hibernate.Datastore;
> import cirrus.hibernate.Session;
> import cirrus.hibernate.SessionFactory;
> import cirrus.hibernate.Hibernate;
>
> import javax.servlet.*;
> import javax.servlet.http.HttpServletRequest;
> import java.io.IOException;
>
> public class HibernateFilter implements Filter
> {
>  static org.apache.log4j.Category log =
> org.apache.log4j.Category.getInstance(HibernateFilter.class.getName());
>
>  private Datastore datastore;
>  private SessionFactory sessions;
>
>  public void doFilter(ServletRequest request, ServletResponse response,
> FilterChain chain) throws IOException, ServletException
>  {
>   try
>   {
>    // Get the http session id from the request, then we will try to get
the
> Hiberate
>    // Session from the request.  If it doesn't exist, then we will create
> it, otherwise
>    // we will use the one that already exists.
>    String vSessionId =
> ((HttpServletRequest)request).getSession(true).getId();
>    Session vSession = (Session)request.getAttribute(vSessionId);
>
>    if (vSession == null)
>    {
>     vSession = sessions.openSession();
>     request.setAttribute(vSessionId, vSession);
>
>     if (log.isDebugEnabled())
>     {
>      log.debug("Opened hibernate session.");
>     }
>    }
>   }
>   catch (Exception exc)
>   {
>    log.error("Error opening Hibernate session.", exc);
>   }
>
>   try
>   {
>    chain.doFilter(request, response);
>   }
>   finally
>   {
>    try
>    {
>     String vSessionId =
((HttpServletRequest)request).getSession().getId();
>     Session vSession = (Session)request.getAttribute(vSessionId);
>
>     // Only try to close the connection if it is open, since it might have
> been
>     // closed somewhere else by mistake.
>     if (vSession.isOpen())
>     {
>      vSession.close();
>
>      if (log.isDebugEnabled())
>      {
>       log.debug("Closed hibernate session.");
>      }
>     }
>    }
>    catch (Exception exc)
>    {
>     log.error("Error closing Hibernate session.", exc);
>    }
>   }
>  }
>
>  public void init(FilterConfig aConfig) throws ServletException
>  {
>   // Initialize your datastore
>   datastore = Hibernate.createDatastore();
>
>   // Initialize your the object -> db mappings
>   // ...
>
>   // Initialize your session
>   sessions = datastore.buildSessionFactory();
>  }
>
>  public void destroy()
>  {
>
>  }
> }
>
>
>
> ----- Original Message -----
> From: "Christoph Sturm" <[EMAIL PROTECTED]>
> To: "Brad Clow" <[EMAIL PROTECTED]>;
> <[EMAIL PROTECTED]>
> Sent: Tuesday, August 27, 2002 10:47 AM
> Subject: Re: [Hibernate-devel] mvc & lazy loading
>
>
> > Hi Brad!
> >
> > This subject is an interesting one that I was also thinking of lately.
> > I did a test app with maverick (mav.sourceforge.net), and there it was
> > really easy. If the controller(=model) implements ModelLifetime, a
discard
> > function is called when the views are finished and the model is
discarded.
> > There I closed my session. Other frameworks that just forward to the
view
> > dont offer this functionality.
> > For most of my stuff I use webwork, so I'd like a solution that works
> there
> > too. I was thinking of closing the session in the finalize method of my
> > controller, but then I dont really know when the session will be closed.
> > Another possibility would be to to pass the session to velocity, and
close
> > it in the velocity view servlet after all is rendered.
> >
> > How did you implement it?
> >
> > regards
> >  chris
> > ----- Original Message -----
> > From: "Brad Clow" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Tuesday, August 27, 2002 12:38 AM
> > Subject: [Hibernate-devel] mvc & lazy loading
> >
> >
> > >
> > > to date, we have avoided using lazy loading when writing a web app in
> > > one of the standard mvc type frameworks (eg. struts, webwork, etc).
> > > this is because objects r typically retrieved, placed in the request
> > > attributes, session closed and control is then passed to the view
(JSP,
> > > velocity, etc).  if the view attempts to access a lazy loaded
collection
> > > in one of the objects an exception is thrown as the associated session
> > > is closed.
> > >
> > > what do other people do?
> > >
> > > yesterday, i spent a few hours writing a very simple webapp framework
> > > that uses velocity for the view.  it enables the velocity rendering to
> > > be done while a session is open.  so far this is working quite well
for
> > > us.
> > >
> > > comments?
> > >
> > > brad
> > >
> > > > _______________________________
> > > > brad clow
> > > > chief technical officer
> > > > workingmouse
> > > >
> > > > email: [EMAIL PROTECTED]
> > > > web:   http://www.workingmouse.com
> > > >
> > >
> > >
> > > -------------------------------------------------------
> > > This sf.net email is sponsored by: OSDN - Tired of that same old
> > > cell phone?  Get a new here for FREE!
> > >
> >
>
https://www.inphonic.com/r.asp?r____________________________________________
> > ___
> > > Hibernate-devel mailing list
> > > [EMAIL PROTECTED]
> > > https://lists.sourceforge.net/lists/listinfo/hibernate-devel
> > >
> >
> >
> >
> > -------------------------------------------------------
> > This sf.net email is sponsored by: OSDN - Tired of that same old
> > cell phone?  Get a new here for FREE!
> > https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
> > _______________________________________________
> > Hibernate-devel mailing list
> > [EMAIL PROTECTED]
> > https://lists.sourceforge.net/lists/listinfo/hibernate-devel
> >
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by: OSDN - Tired of that same old
> cell phone?  Get a new here for FREE!
> https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
> _______________________________________________
> Hibernate-devel mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/hibernate-devel
>



-------------------------------------------------------
This sf.net email is sponsored by: OSDN - Tired of that same old
cell phone?  Get a new here for FREE!
https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
_______________________________________________
Hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to