Results 1 to 4 of 4

Thread: Keep Hibernate Session open per handleRequest NOT in View?

  1. #1

    Default Keep Hibernate Session open per handleRequest NOT in View?

    Hi,

    first: i don't and don't want to use OpenSessionInView (OSIV). This would be the easiest solution to my problem. But for several reasons i don't want to use it (and should be discussed elsewhere)

    I have a service layer using Spring 2.0 transaction advice (<aop:advisor> and <tx:advice>).

    Imagine a service layer with two methods:
    Code:
     getMostRecentElement() 
     getElements()
    My Controller needs both of them:

    Code:
    Object formBackingObject (...) {
      return service.getMostRecentElement();
    }
    
    Map referenceData(...) {
      return service.getElements()
    }
    ServiceLayer has a repostiory injected which extends HibernateDaoSupport.

    the calls to
    • getMostRecentElement()
    • getElements()

    both start a transaction and grab a new session. Therefore without a second level cache hibernate will query the db two times to get all elements as getMostRecentElement() iterates over all elements.

    I just want to stop hibernate from querying the dB twice. So i want to keep my session open while handleRequest is executed. I don't want to use 2nd level cache for it as these elements will only be of use in this request. The easiest thing would be to keep the session open and use hibernates 1st level cache.

    I could make my handleRequest transactional, but i don't think that transactional Controllers are a good thing.

    As i didn't found anything in forum or web about my problem i rather think it is not a good approach. Maybe i misunderstood something. if so, please tell me why, if not please tell me how to keep the session open.

    please, enlighten me. Your help is very appreciated.

    kind regards
    Janning

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    You'd need to open a Session and bind it to threadLocal for the duration of the two calls, thus using the same Session. That's what OpenSessionInView would do, likewise making the controller transactional (which I also agree would be a bad idea).
    Last edited by karldmoore; Aug 30th, 2007 at 05:20 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3

    Default

    Quote Originally Posted by karldmoore View Post
    You'd need to open a Session and bind it to threadLocal for the duration of the two calls, thus using the same Session. That's what OpenSessionInView would do, likewise making the controller transactional (which I also agree would be a bad idea).
    karldmoore, thanks for your reply. But how do i bind my (Hibernate) Session to a threadLocal? I am quite new to all this stuff, any hint is very appreciated.

    As is use an assembly phase before returning data to the controller, i don't need an open session in the view. And i don't want to use OSIV because it get used if available and i want to close the session as soon as possible.

    Isn't it like everybody who is not using OSIV needs something like this to enable a working firstLevel cache?

    Why isn't there something like "OpenSessionInControllerInterceptor"?

  4. #4

    Default OpenSessionInControllerInterceptor?

    i just looked at the SourceCode of OpenSessionInViewInterceptor and in my scenario i could close the session in postHandle

    Couldn't it be done like this:

    Code:
    public class OpenSessionInViewControllerInterceptor extends  OpenSessionInViewInterceptor {
    
      @Override
      public void postHandle(WebRequest request, ModelMap model) throws DataAccessException {
    
        super.postHandle(request, model);
        super.afterCompletion(request, exeption);
    
      }
    }
    It just closes the session by calling "afterCompletion" which is usally done only when the thread completes, but i want to close the session when the controller has finished.

    Does it make any sense?

    I rather would like to know which is best practice as i fear uncommon solutions to common problems.
    Last edited by janning; Jun 17th, 2007 at 03:33 AM. Reason: typos

Posting Permissions

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