I think this is a good solution, except I think you forgot to save the
Session if it's null. I would optimize it by having two methods:

getReadOnlySession
getReadWriteSession

Which both return the same Session, but getReadWriteSession sets a flag
to do the flush()... If the flag is not set, you can skip the flush and
roll back the transaction.

Jason

> -----Original Message-----
> From: Matthew E. Porter [mailto:[EMAIL PROTECTED] 
> Sent: Friday, November 14, 2003 12:16 PM
> To: [EMAIL PROTECTED]
> Subject: [OS-webwork] Hibernate Session as Request Scoped Component
> 
> 
> Instead of using a ThreadLocal Hibernate Session, has anyone played 
> with having a HibernateSessionProvider component at the 
> request scope?  
> Here is how I would envision the code:
> 
> public class HibernateSessionProvider implements SessionProvider, 
> Disposable {
>      private SessionFactoryProvider sessionFactoryProvider;
>      private Session session;
> 
>      public SessionFactoryProvider getSessionFactoryProvider() {
>          return this.sessionFactoryProvider;
>      }
> 
>      public void setSessionFactoryProvider(SessionFactoryProvider 
> sessionFactoryProvider) {
>          this.sessionFactoryProvider = sessionFactoryProvider;
>      }
> 
>      public Session getSession() throws PersistenceException {
>          if ((this.session == null) || (!this.session.isOpen())) {
>              try {
>                  return 
> this.sessionFactoryProvider.getSessionFactory().openSession();
>              } catch (HibernateException he) {
>                  throw new PersistenceException("HibernateException 
> caught building hibernate hibernate", he);
>              }
>          }
> 
>          return this.session;
>      }
> 
>      public void dispose() {
>          if ((this.session != null) && (this.session.isOpen()) && 
> (this.session.isConnected()) {
>              try {
>              this.session.flush();
>              this.session.connection().commit();
>              this.session.close();
>              } catch (Exception e) {
>                  // do something
>              }
>          }
> 
>      }
> }
> 
> 
> Is there anything wrong with doing it this way?  All of the 
> components 
> needing a Hibernate session (Persistence Manager and DAOs) are scoped 
> at the same level.  SessionFactoryProvider is another component but 
> scoped at the application.
> 
> 
> Cheers,
>    matthew
> 
> 
> 
> -------------------------------------------------------
> This SF. Net email is sponsored by: GoToMyPC
> GoToMyPC is the fast, easy and secure way to access your 
> computer from any Web browser or wireless device. Click here 
> to Try it Free! 
> https://www.gotomypc.com/tr/OSDN/AW/Q4_2003/t/g22lp?Target=mm/
g22lp.tmpl
_______________________________________________
Opensymphony-webwork mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/opensymphony-webwork


-------------------------------------------------------
This SF. Net email is sponsored by: GoToMyPC
GoToMyPC is the fast, easy and secure way to access your computer from
any Web browser or wireless device. Click here to Try it Free!
https://www.gotomypc.com/tr/OSDN/AW/Q4_2003/t/g22lp?Target=mm/g22lp.tmpl
_______________________________________________
Opensymphony-webwork mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/opensymphony-webwork

Reply via email to