• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate, Transactions and DAO's

 
joe black
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a bunch of DAO's which have methods such as:
findUserById, persistUser, findUserByEmail, etc

Now, most methods simply involve running a single hql in
a try, catch, finally block:

try
{
// open session and find by id
}
catch( HibernateException e
{
// throw an infrastructure exception
}
finally
{
// close the session
}

So in my service layer I use these dao's in conjunction,
but this doesn't work when I need transactions. How should I handle
transactions in my code? SHould I jsut have large blocks of code in the service with rollbacks, etc/. I wanted to use the different dao's in conjunction with eachother in the service layer, but not sure how transactions fit in. I would like to incorporate Spring to help with the transactions.

[ July 21, 2006: Message edited by: Joe Black ]
[ July 21, 2006: Message edited by: Joe Black ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well when you get a Session, you call getTransaction() then call beginTrasaction on the returned Transaction object. If the catch is where you will probably have your rollback(), and the end of the code within the try right before the session.close() have a commit().

Then you will use the Hibernate "transaction" If you have a JTA Transaction already either through EJB or Spring, then Hibernate will automatically use that transaction context.

Mark
 
joe black
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea I know how to get the Transaction and how to use it, but my dao methods are very fine grained, so by themselves they dont need transaction support, but when called together, they do sometimes. Lets say in a service layer, I called updateUser, updateAccount, deleteUser. If I called any of those alone, I don't need transactions. But called together, even if I put the transaction code into those methods, I dont think it would work. For example, if it failed at the second update, the dao method would do a rollback, but the first update, updateUser, would've still been commited right? I know I could create some giant method that does both updates and the delete, but I would rather not, since I also have use cases where I need to call those methods by themselves. This is more of a design question I guess.
[ July 22, 2006: Message edited by: Joe Black ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your code that calls all the service layer gets a UserTransaction and starts one, if one Dao works and the next one fails, everything will be rolled back from the UserTransaction. Hibernate will use the global UserTransaction that you created.

Mark
 
joe black
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses. After doing some research, I decided to go with Spring's declarative transaction managment. This way I don't have to change my code.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i would suggest you use a framework like spring.
it handles transactions either in the XML files or trough annotations. you can define levels, when to rollback, when to commit and so on.
It uses AOP for the TX proxies and ThreadLocals to pass around the TX.

cheers

pascal
 
Dhananjay Inamdar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,

I would like to know what are the changes you have done to solve your problem?

Initially you were doing a transaction programatically and now you were using Spring's declartive transaction. At the end both are same, Spring only take out the transaction demarcation out of Java code to XML files.

Then how this Spring's declarative transaction is solving your problem. Please explain in detail.

Thanks in advance!


Regards,
DJDON
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic