Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate transactions

 
G Mehta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using struts and hibernate. My design is

struts-->Business Service(Logic)-->DAOService(Per POJO)-->Hibernate

I have few POJO objects that hibernate will use to store data into DB, e.g. pojo object User, Product, Catalog

I have a DAOService class per POJO to provide CRUD. This will use a Session Object of hibernate to save data to database. In the DAOService it just does a 'save' and does not have any transaction control so there are no beginTransaction/commit/rollback statements in these classes.

I was thinking that Business Service(logic) layer will control the transactions as this is where the business logic/processes are carried out.
So in this layer I would create a Session Object and do beginTransaction/commit/rollback. .... What I want is to use this same Session object in the DAOService layer and not having to create more Session object within the DAOService layer.

Obviously I can pass the Session object as a parameter to each method call to DAOService layer. But is this efficient or is there a better way to do this?

Is there a problem with Threading issues as I will use the same session object?

Help would be appreciated.

Thanks
GM
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Obviously I can pass the Session object as a parameter to each method call to DAOService layer. But is this efficient or is there a better way to do this?

It is the recommended pattern to use. Hibernate is designed to work normally with a Session per business transaction. Its not that expensive using a Session per DB operation if you would like, but Hibernate's design is such that you would loose a lot of the benefit of the first level cache doing this.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option would be to use the ThreadLocal Session pattern, i.e. store a Session object into a ThreadLocal variable somewhere and have your DAO classes ask for the session with "HibernateSessions.currentSession()".
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"GM72"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic