Results 1 to 3 of 3

Thread: OpenSessionInView and Transactions

  1. #1
    Join Date
    May 2005
    Posts
    14

    Default OpenSessionInView and Transactions

    Hi,

    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?

    Regards,
    Pawel KOzlowski

  2. #2
    Join Date
    Aug 2004
    Location
    Southampton, UK
    Posts
    826

    Default

    Pawel,

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

    Rob
    Rob Harrop
    Lead Engineer, dm Server
    SpringSource
    http://www.springsource.com

    Co-Author - Pro Spring

  3. #3

    Default clarify

    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:

    Code:
    public EditCategoryFormController() {
          setSessionForm(true); // needed to maintain the same form backing object
          setBindOnNewForm(true);
          setCommandName("categoryForm");
          setFormView("category-edit");
       }
    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:

    Code:
    public static org.hibernate.Session reassociateEntity(ServletContext sc, 
                Object obj) { 
            // Remember to call sessionFactoryUtils.closeSessionIfNecessary( 
            SessionFactory sessionFactory = (SessionFactory) WebApplicationContextUtils 
                    .getWebApplicationContext(sc).getBean("sessionFactory"); 
            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.

Similar Threads

  1. OpenSessionInView good or evil?
    By bullgod in forum Data
    Replies: 3
    Last Post: Oct 12th, 2009, 10:36 AM
  2. OpenSessionInView for many databases
    By thuss in forum Data
    Replies: 3
    Last Post: Nov 18th, 2005, 09:56 AM
  3. Replies: 9
    Last Post: Aug 16th, 2005, 04:55 PM
  4. Simulate OpenSessionInView in JUnit?
    By cbredesen in forum Data
    Replies: 7
    Last Post: Mar 31st, 2005, 07:14 AM
  5. Replies: 2
    Last Post: Oct 19th, 2004, 03:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •