The moose likes Object Relational Mapping and the fly likes Where to open Hibernate Session and Transactions Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Where to open Hibernate Session and Transactions" Watch "Where to open Hibernate Session and Transactions" New topic
Author

Where to open Hibernate Session and Transactions

Kashif Mughal
Ranch Hand

Joined: Jun 19, 2008
Posts: 44
Hi
I want to know where is the best to open the Hibernate session with spring MVC application,

in Controllers, Service Layer or at DAO level.

I have opened a session and transaction at service level it works fine for the first time when i login but when i try to perform next operation Say Delete, I get the following exception



Service Layer


DAO Level



Thanks in Advance,
Kash.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4923

This is a very common conundrum, and a question many people struggle with.

The most common response from people much better looking than me is to use the Open Session in View strategy, which bacically means you allow your client to open the session and demarcate the transaction, leaving the session open even during the view phase, and thus eliminating the chance of any LazyInitializationException from happening.

Here's a little tutorial from Hibernate.org that discusses the topic:


Open Session in View Design Strategy from hibernate.org

-Cameron McKenzie

This message was edited 1 time. Last update was at by Cameron Wallace McKenzie



Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
Joao Victor Ribeiro
Greenhorn

Joined: Jul 06, 2008
Posts: 3
i see you're using spring mvc. So why don't you consider using spring AOP to control the transaction at the service layer ??

see: http://static.springframework.org/spring/docs/2.5.x/reference/orm.html#orm-hibernate-tx-declarative
Andy Grove
Greenhorn

Joined: Nov 11, 2003
Posts: 18
Here's a Spring/Hibernate tutorial that might be useful - it includes complete configuration files and code. It shows how to use control transactions (from the web tier in this example) and uses annotations on the DAO classes for Spring Transactions.

http://www.codesuccess.com/tutorials/spring_hibernate
Kashif Mughal
Ranch Hand

Joined: Jun 19, 2008
Posts: 44
Thanks Andy abd Jao, They both are good sources to read.
Can you please suggest me in which situation we should use hibernate.current_session_context_class configuration strategy as thread and managed. As we have to pick one if I am not wrong.
Kashif Mughal
Ranch Hand

Joined: Jun 19, 2008
Posts: 44
Thanks Andy and Joao, They both are good sources to read.
Can you please suggest me in which situation we should use hibernate.current_session_context_class configuration strategy as thread and managed. As we have to pick one if I am not wrong.
Neeraj Kumar Sharma
Greenhorn

Joined: Nov 09, 2010
Posts: 1
Its is good to handle the seession and transaction DAQ layer only
pradeep gamage
Ranch Hand

Joined: Aug 03, 2009
Posts: 65

oh...
I am currently developing spring hibernate web application. I Use Hibernate Session in Dao level and Transaction in Service level.



Software Engineer(BSC):SCJP 1.5
(Knowledge is power when applied)
Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 111

Always try to separate transaction demarcation code from you business logic. Never execute transaction code with the code that run queries or make some business logic decisions.

First implement some TransactionOrchestrator that does the following

ORCHESTRATOR_BEAN
OpenTransaction
Execute method of one of you business class that implement a common interface. For example MyProgramInterface.execute(DomainObject object)
Close transaction if success
Rollback transaction if failure.

It gives you a flexibility to write transaction code only once. You can have million of beans that use ORCHESTRATOR_BEAN as their parent. Only what they need to replace is a reference to implementation of MyProgramInterface.
You program interface does not care if it runs in transaction or not. Or how transaction was opened. Only thing it needs to do in case of failure is throw an exception to let your ORCHESTRATOR_BEAN that it needs to rollback a transaction



Good luck for yourself.
knazeer ahmed
Greenhorn

Joined: Sep 09, 2008
Posts: 7
Generally we will open the session the Hibernate Session in DAO layer. Because when you finish storing/retrieving the records it will be easy to close the session in DAO.
Reason:
if you have opened the session in service layer or other layer and you store/try to retrive the code and you got some exception with your business logic the session will not be closed because of that error/exception. so the connection will not be closed until and unless you are closing the connections in service or other layer later.


So better to open the session in DAO.

Thanks
Nazeer
James Boswell
Ranch Hand

Joined: Nov 09, 2011
Posts: 139
I always feel it is best to handle transactions from the service (business) layer.

The reason for this is simple. It is likely that a single business operation will need to call multiple DAO methods. This operation will be a single unit of work (i.e. a transaction). If one of the invoked DAO methods throws an exception, the entire transaction can be rolled back in the service layer which catches the exception. This cannot be achieved if you start and end a transaction for each DAO method.

This message was edited 1 time. Last update was at by James Boswell

Mandy Rocks
Greenhorn

Joined: Nov 13, 2011
Posts: 3
Defining a start of any transaction depends on Application Architecture. One can not say start point of Transaction at any layer. We believe that, Architecture very well aware of boundary of any transaction exists in Application.
 
 
subject: Where to open Hibernate Session and Transactions
 
developer file tools