File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Where to open Hibernate Session and Transactions" Watch "Where to open Hibernate Session and Transactions" New topic

Where to open Hibernate Session and Transactions

Kashif Mughal
Ranch Hand

Joined: Jun 19, 2008
Posts: 44
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,
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

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 that discusses the topic:

Open Session in View Design Strategy from

-Cameron McKenzie
Joao Victor Ribeiro

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 ??

Andy Grove

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.
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

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: 85

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)
Stan Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 120

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

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

Joined: Sep 09, 2008
Posts: 8
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.
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.

James Boswell

Joined: Nov 09, 2011
Posts: 1051

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.
Mandy Rocks

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.
Cj Recto

Joined: Mar 02, 2012
Posts: 26
First of all, the way you approach session and transaction using HibernateUtil is kinda old school. Since you are using Spring framework, you should let spring handle the session and transaction using spring annotation.

To answer your question,
It is true that you can open session and transaction in any layer of your application, but since you are asking for the best place where to open session/transaction, then the answer is
hibernate sessions is best place in DAO layer, while hibernate Transactions is best place in the service layer.
Stan Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 120

First let make clear.
There are two concepts
OLD WAY: Bound hibernate session to thread. One thread - one session.
NEW WAY. Bound hibernate session to transaction. One transaction - one hibernate session.

If you chose the old way - you are on your own, not gonna advice anything but most common approach create a transaction the same place thread get connected first time to your code (Servlet, StrutsAction, SpringController, WebService Endpoint - plenty of choices but all driven by a controller framework)

If you chose a new way then you are going to use AOP. You will tell to which classes and methods transactions have to be created. Here I give you few advices

1. Start from defining interfaces. AOP only works with interfaces so if you have a class/method reference that a new transaction should start - not gonna work. Use only interface references.

2. Every DAO method should be able to start transaction by using transaction demarcation (REQUIRED/SUPPORT). You need it for unit testing. Never (Never !) in production transaction shell be started by
direct call to DAO but for running unit tests you have to keep a gate open. So ideally you will have two configurations one for prod, another one for unit testing

3. For every transaction as people above say you have to define an orchestrator. For me it is just a dummy Spring bean that has million of definitions - each invokes a program that implements the same interface.

Pointcut saying my orchestrators need a transaction

And I have a million of beans based on the same orchestrator class

Orchestrator here is just a class that invokes a program that might need a transaction. It validates a form, convert form parameters into domain object and passes them to program that has to handle a request. Spring AOP makes sure that transaction is created and commited/rolled back once orchestration is completed
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
i am using Hibernate with Swing. Its a windows application. Where should i open sessions and transactions?
(Sorry, if i should have created a separate topic for this doubt)

Pablo Abbate
Ranch Hand

Joined: Aug 06, 2012
Posts: 30


I have used spring+hibernate in several swing applications and the approach is pretty much the same than web applications. Open the sessions at DAO level and set the transactions at service layer. The UI is implemented by panels and forms and you should use the service layer to access data and to implement your business logic.

One useful question is what would happen if you have to change your Application from Swing to Web (or vice versa). The answer should be "just change the UI". Keep that in mind and your architecture will be fine.

Nissi Group, Posicionamiento Web, Desarrollo de Software, DiseƱo Web
Blog , LinkedIn
I agree. Here's the link:
subject: Where to open Hibernate Session and Transactions
It's not a secret anymore!