-->
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.  [ 3 posts ] 
Author Message
 Post subject: Beginners question: why use transactions?
PostPosted: Mon Mar 02, 2009 3:52 am 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
Hi,

I've become familiar with the basics of Hibernate through some various online tutorials. However, there's one concept which none of the tutorials introduce, but which often is mentioned on this forum: transactions.

What is the consequence of not using transactions, but simply performing the whole SELECT/UPDAT/DELETE process withouth commencing a transaction? For instance, a tutorial gives this example:

Code:
(1) get session from session factory
(2) perform database action
(3) flush session (on UPDATE and DELETE)
(4) close session


There isn't any transactions involved here. However, on the other side, everything i've been trying out works just fine withouth transactions.

So, my question would be: why would i use transactions and what good do they do?


Top
 Profile  
 
 Post subject: Re: Beginners question: why use transactions?
PostPosted: Mon Mar 02, 2009 4:12 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
sbrattla wrote:
there's one concept which none of the tutorials introduce


http://www.google.com/search?q=hibernat ... ransaction

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 5:00 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Well... as long as everything works you will often not notice a difference between using a transaction or not. If things start to go badly you will see a big difference. This is because a single operation may actually be made up of several SQL statements. If you use transactions it doesn't matter where in the sequence things start to go wrong, since the entire transaction is rolled back and your database is returned to the state it was in before the first SQL was executed. If you don't use transactions your database can be left in an inconsistent state since only some SQL statements was processed. A simple case is the traditional money transfer between two accounts. In it's simplest form it involves two SQL statements, one that decreases the amount in the FROM account and one that increases the amount in the TO account. Imagine what would happen if only one of the statements was successful!

This is just one case were transactions are useful. They are also needed when you have multiple clients accessing the same database at the same time. In most cases, a client doesn't want to see changes from other clients until the entire operation has completed successfully. Using transactions is a way to control that.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.