Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Hibernate transactions

 
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE 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.
 
author
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()".
 
ranger
Posts: 17346
11
Mac IntelliJ IDE 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
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic