Difference between session per request and Open Sesson in view pattern

1.9k views Asked by At

Folks, what's the difference between Open Session in View and Session per request pattern? I am using Spring MVC and Hibernate . I am not talking about the transaction demarcation here, for Session per request seems to be 1 session:1 tx. But for OSIV, there is 1 session and then there are multiple transactions(1 for each service call). Can someone please shed some light on this?

1

There are 1 answers

1
user1417684 On BEST ANSWER

The definining characteristic of OSIV isn't really the multiple transactions, but rather that the session remains open while the view is being rendered. This page on the JBoss wiki illustrates this setup (though managing transactions manually from the web layer is not my cup of tea... for something more pre-packaged, see Spring's OpenSessionInViewFilter).

There is one session, one transaction. This is stil OSIV, because all the work, including rendering the view is done inside the scope of the session. Note that even if you mark all your service methods as @Transactional (or the XML equivalent), they will still participate in the same single transaction unless they specifically require a new transaction to be started.

So OSIV can be a type of session-per-request, and they are sometimes used interchangeably. But I would say that it is possible to have a session per request implementation that is not OSIV, if you make sure to close your single session prior to view rendering.