-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: Where do I begin transaction and commit
PostPosted: Fri Aug 17, 2007 4:20 am 
Beginner
Beginner

Joined: Sun Aug 12, 2007 11:22 am
Posts: 44
Location: Sweden
Where do I begin transaction and commit?

I have created this DAO class for my user. Should I begin a transaction and commit in that method or should it be outside of the dao?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 4:33 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You should begin the transaction before your "unit of work" starts. Typically this is outside of the DAO. Consider this example unit of work... you want to delete a user, delete their preferences and write a log entry to the dabase. These must all happen within the same transaction but would probably use separate DAOs for the different operations. Hence, start the tx outside the DAO in this case.


Top
 Profile  
 
 Post subject: transaction can be started anywhere before commit
PostPosted: Fri Aug 17, 2007 6:12 am 
Newbie

Joined: Tue Aug 24, 2004 5:36 pm
Posts: 16
It sounds crazy, but it is true. Try this :

Transaction tx1 = hibernateSession.beginTransaction();
hibernateSession.load(myUser, oid);
myUser.setCredit(BigDecimal.valueOf(9999L));
tx1.rollback();

Transaction tx2 = hibernateSession.beginTransaction();
tx2.commit();

myUser will have credit 9999 saved to database!

This means where you begin the hibernate transaction does not matter, but the point you commit have to be after the object to get in a persisted state (not transient).

Let's make another try:

Transaction tx1 = hibernateSession.beginTransaction();
tx1.commit();
hibernateSession.load(myUser, oid);
myUser.setCredit(BigDecimal.valueOf(9999L));

that will not save the credit to the database.

The consequence of this test is that if you try to do multi-transaction, you have to be very careful.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 6:56 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Code:
Transaction tx1 = hibernateSession.beginTransaction();
hibernateSession.load(myUser, oid);
myUser.setCredit(BigDecimal.valueOf(9999L));
tx1.rollback();

Transaction tx2 = hibernateSession.beginTransaction();
tx2.commit();

This code isn't valid because when you rollback a tx you must close the associated session. This happens automatically on rollback when using thread base session management.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 7:55 am 
Beginner
Beginner

Joined: Sun Aug 12, 2007 11:22 am
Posts: 44
Location: Sweden
Thanks!

If the transaction shouldn't be in the dao how should I implement it in a nice way? Let's say I use Wicket with this. I dont want to access the dao's and handle transaction in a wicket class. Design question but how do you handle this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 8:04 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
There are many ways of course. Recently I've been using the springframework to handle my transaction management. Its simple, powerful, well documented and hence really popular. Great hibernate support too.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 8:39 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://hibernate.org/42.html
http://hibernate.org/43.html

You do not need Spring, in fact, Spring makes everything more complicated when it comes to Hibernate Session management. Its Hibernate support is extremely poor (I really don't know why people think it isn't...) with legacy and outdated classes like HibernateTemplate and hard dependencies on the Spring framework for writing trivial DAOs (Note that these problems are typically the result of bad documentation and tutorials though, not inherently of the Spring framework design).

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 8:50 am 
Newbie

Joined: Tue Aug 24, 2004 5:36 pm
Posts: 16
thatmikewilliams wrote:
Code:
Transaction tx1 = hibernateSession.beginTransaction();
hibernateSession.load(myUser, oid);
myUser.setCredit(BigDecimal.valueOf(9999L));
tx1.rollback();

Transaction tx2 = hibernateSession.beginTransaction();
tx2.commit();

This code isn't valid because when you rollback a tx you must close the associated session. This happens automatically on rollback when using thread base session management.


Where come your confirmation? Hibernate does not reject this code with any exception.

In the context of the Hibernate conversational session, this scenario could happen even one should avoid it. That's why one should not put transaction in the DAO.

From a pratical point of view, Seam is a better candidate than Spring for pure DAO stuff. Spring is too havy.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 9:06 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
In the context of the Hibernate conversational session, this scenario could happen even one should avoid it.


No, it does not happen if you designed your application properly because rollbacks are occurring in a catch{} clause somewhere, and the session is closed in the finally{}.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 9:49 am 
Beginner
Beginner

Joined: Sun Aug 12, 2007 11:22 am
Posts: 44
Location: Sweden
Thank you all. I think I got where the transaction should go!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 9:49 am 
Newbie

Joined: Tue Aug 24, 2004 5:36 pm
Posts: 16
I think that the transaction is the unit of work and not the session. so it is possible that in a session, you have n transactions and Ti rollbacked and Tj committed for i<j. So one unique "finally" cannot deal with this.

I know Hibernate does not handle by default this situation.

All I said is Hibernate does not throws any exception with multi-transactions if I leave hibernate.transaction.auto_close_session to its default value as the Hibernate reference document suggested.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 1:03 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Of course it works just the same way if you have multiple transactions over one session! If one of the transactions rolls back, the session is closed, conversation is over, fine.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.