Announcement Announcement Module
No announcement yet.
OpenSessionInViewFilter v OpenSessionInViewInterceptor v @Transactional Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenSessionInViewFilter v OpenSessionInViewInterceptor v @Transactional

    I have a servlet that calls and uses hibernate objects. It was throwing lazyinitializationexceptions and after a bit of reading I found there are 3 options:

    Seems to be on the way out (replaced by the more general OpenSessionInViewInterceptor). OpenSessionInViewFilter is the same as
    OpenSessionInViewInterceptor only it can only be used with servlets.

    General case transaction interceptor which can be used for other technologies (JMS etc) not just DBs.

    AOP proxies

    I wrote a blog on my finding.

    LazyInitializationException in servlet

    I chose OpenSessionInViewInterceptor because it seemed easiest to configure.
    I did not choose OpenSessionInViewFilter because I couldn't find it in the reference manual.
    I did not choose @Transactional because it increased the call stack by about 10 layers.

    I was wondering if there were any pros and/or cons that could help me decide if I have made the right choice.

  • #2
    Does anyone know?


    • #3
      Where to demarcate transactions

      You're getting a couple of things mixed up here. OSIV is a way to control when your Hibernate session gets closed; normally it will close when the transaction ends, but OSIV lets you keep one open until the view has finished rendering so that you can navigate lazy-loaded properties.

      None of this affects the fact that your service layer methods should each start and finish a transaction. Whether you achieve this using the @Transactional annotations or some other way (see the reference docs) is another question.

      As I've commented on your blog, your web controllers should
      definitely not be concerned with starting transactions.


      • #4
        Firstly, thanks for the blog comments. I will add any conclusions from this thread to the blog to help others.

        So, if I shouldn't put @Transactional in front of my web controllers, should I be using OpenSessionInViewInterceptor instead, or are you suggesting that I write transaction cutpoints in xml for the same point to separate the configuration from the web layer.


        • #5
          Originally posted by andrews View Post
          ... Whether you achieve this using the @Transactional annotations or some other way (see the reference docs) is another question.
          No, it's not! That's this question.