From: Brad C. <bra...@wo...> - 2002-08-26 22:39:05
|
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 >=20 > email: bra...@wo... > web: http://www.workingmouse.com >=20 |
From: Christoph S. <ch...@mc...> - 2002-08-27 08:49:29
|
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" <bra...@wo...> To: <hib...@li...> 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: bra...@wo... > > 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 > Hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel > |
From: Jon L. <jon...@xe...> - 2002-08-27 09:46:22
|
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" <ch...@mc...> To: "Brad Clow" <bra...@wo...>; <hib...@li...> 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" <bra...@wo...> > To: <hib...@li...> > 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: bra...@wo... > > > 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 > > Hib...@li... > > 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 > Hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel > |
From: Brad C. <bra...@wo...> - 2002-08-31 05:33:05
|
jon, this seems like a pretty neat way of doing it. do u use <filter-mapping> elements in the web.xml to control which requests it gets applied to - so u only have a session created for requests that actually need one? brad ----- Original Message ----- From: "Jon Lipsky" <jon...@xe...> To: "Christoph Sturm" <ch...@mc...>; "Brad Clow" <bra...@wo...>; <hib...@li...> Sent: Tuesday, August 27, 2002 7:46 PM 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" <ch...@mc...> > To: "Brad Clow" <bra...@wo...>; > <hib...@li...> > 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" <bra...@wo...> > > To: <hib...@li...> > > 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: bra...@wo... > > > > 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 > > > Hib...@li... > > > 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 > > Hib...@li... > > https://lists.sourceforge.net/lists/listinfo/hibernate-devel > > > > |
From: Jon L. <jon...@xe...> - 2002-09-02 11:40:11
|
Brad, This is exactly what I do... I also have a couple different versions of my filter that handler their own URL filtering using the Apache ORO library. I do this so I can more complex regular expressions for the the URL matching... Jon... ----- Original Message ----- From: "Brad Clow" <bra...@wo...> To: <hib...@li...> Sent: Saturday, August 31, 2002 7:38 AM Subject: Re: [Hibernate-devel] mvc & lazy loading > jon, > > this seems like a pretty neat way of doing it. do u use <filter-mapping> > elements in the web.xml to control which requests it gets applied to - so u > only have a session created for requests that actually need one? > > brad > > > ----- Original Message ----- > From: "Jon Lipsky" <jon...@xe...> > To: "Christoph Sturm" <ch...@mc...>; "Brad Clow" > <bra...@wo...>; <hib...@li...> > Sent: Tuesday, August 27, 2002 7:46 PM > 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" <ch...@mc...> > > To: "Brad Clow" <bra...@wo...>; > > <hib...@li...> > > 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" <bra...@wo...> > > > To: <hib...@li...> > > > 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: bra...@wo... > > > > > 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 > > > > Hib...@li... > > > > 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 > > > Hib...@li... > > > 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 > Hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel > |
From: Christoph S. <ch...@mc...> - 2002-09-03 10:23:36
|
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" <jon...@xe...> To: "Christoph Sturm" <ch...@mc...>; "Brad Clow" <bra...@wo...>; <hib...@li...> 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" <ch...@mc...> > To: "Brad Clow" <bra...@wo...>; > <hib...@li...> > 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" <bra...@wo...> > > To: <hib...@li...> > > 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: bra...@wo... > > > > 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 > > > Hib...@li... > > > 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 > > Hib...@li... > > 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 > Hib...@li... > https://lists.sourceforge.net/lists/listinfo/hibernate-devel > |
From: Jon L. <jon...@xe...> - 2002-09-03 15:41:49
|
Hi Chris, Yes, you are correct... I'm not quite sure what I was thinking when I put that in there... :-) This was a simplified version of my real code to be used an example for all of you, and I guess I made a mistake when I was doing that. What I really meant to express is that you might want to generate an ID that is used as the key to store the Hibernate session in the request. By doing this, you can use multiple instances of the filter to support multiple Hibernate sessions to different databases. Thanks for finding my mistake so I could clarify my original intention of that "vSessionId" variable! Jon... ----- Original Message ----- From: "Christoph Sturm" <ch...@mc...> To: "Jon Lipsky" <jon...@xe...>; <hib...@li...> Sent: Tuesday, September 03, 2002 12:21 PM Subject: Re: [Hibernate-devel] mvc & lazy loading > 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" <jon...@xe...> > To: "Christoph Sturm" <ch...@mc...>; "Brad Clow" > <bra...@wo...>; <hib...@li...> > 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" <ch...@mc...> > > To: "Brad Clow" <bra...@wo...>; > > <hib...@li...> > > 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" <bra...@wo...> > > > To: <hib...@li...> > > > 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: bra...@wo... > > > > > 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 > > > > Hib...@li... > > > > 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 > > > Hib...@li... > > > 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 > > Hib...@li... > > https://lists.sourceforge.net/lists/listinfo/hibernate-devel > > > |