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
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:
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.
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
posted
0
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
posted
0
Its is good to handle the seession and transaction DAQ layer only
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
posted
0
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
posted
0
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
posted
0
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