Announcement Announcement Module
No announcement yet.
OpenSessionInView and Transactions Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenSessionInView and Transactions


    I’m using Spring + Hibernate3 with OpenSessionInView pattern (to lazy-load collections). I would also like to have one transaction per single HTTP Request (that is – effectively have one transaction per Hibernate session). I wonder if it’s possible to achieve this with Spring’s declarative transactions or do I have to provide custom SessionFactory?

    Few more words about app architecture: I’m using Spring MVC with service layer (façade for grouping methods operating on multiple DAOs). I was trying to proxy service interface to have all operations executing inside one transaction, but loading collections doesn’t go through this service layer.

    Please, could someone point me out right direction?

    Pawel KOzlowski

  • #2

    If you want to have one transaction per request, try advising your MVC controller instead of your middle tier services.



    • #3

      Rob -

      I'm also trying to set up the session-per-request and transaction-per-request pattern. I've implemented a controller that extends SingleFormController as described in Pro Spring and I made a custom form object. My constructor looks like:

      public EditCategoryFormController() {
            setSessionForm(true); // needed to maintain the same form backing object
      I advised this controller to use a TransactionProxyFactoryBean, with the HibernateTransactionManager.

      My question is:
      Since I'm using the session to store the "working copy" of my form-backing object, and I'm binding one session per request (with OpenSessionInViewInterceptor), do I need to reattach the form-backing object to the new request's session each time into the controller? Can I do that declaratively somehow?

      If not, I found this code in another post, but I think I will have to tweak it:

      public static org.hibernate.Session reassociateEntity(ServletContext sc, 
                  Object obj) { 
              // Remember to call sessionFactoryUtils.closeSessionIfNecessary( 
              SessionFactory sessionFactory = (SessionFactory) WebApplicationContextUtils 
              org.hibernate.Session ses = SessionFactoryUtils.getSession( 
                      sessionFactory, true); 
              ses.lock(obj, LockMode.NONE); 
              return ses; 
      Is this a good pattern to use, or should I try to wrap each of my service methods in their own transactions, and then use singleSessionMode = false in OSIVInterceptor? The javadoc (Juergen) implies there will be a penalty for using this method since the first level cache is disabled.

      BTW, excellent book. I think you could have doubled the length of the MVC chapter, though... specifically regarding the relationship (and associated tasks) of SimpleFormController's class ancestry.