IntelliJ Java IDE
The moose likes Object Relational Mapping and the fly likes Hibernate, Transactions and DAO's Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Hibernate, Transactions and DAO Watch "Hibernate, Transactions and DAO New topic
Author

Hibernate, Transactions and DAO's

joe black
Ranch Hand

Joined: Dec 03, 2003
Posts: 103
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

Joined: Feb 05, 2001
Posts: 16622

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
joe black
Ranch Hand

Joined: Dec 03, 2003
Posts: 103
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

Joined: Feb 05, 2001
Posts: 16622

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

Joined: Dec 03, 2003
Posts: 103
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

Joined: Jun 19, 2001
Posts: 547
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

Joined: Jan 27, 2003
Posts: 128
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


Just like you, struggeling to get the right solutions!<br /> <br />Sun Certified Java Programmer 1.5<br /> <br />Target - SCWCD
 
 
subject: Hibernate, Transactions and DAO's
 
Threads others viewed
how to logout using sessions
Catching Spring DAO exceptions
DAO implementation questions
Webservices and Request/Response
DAOs X POJOs X FACADEs
developer file tools