-->
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.  [ 4 posts ] 
Author Message
 Post subject: transaction.commit() when session's flush mode is manual?
PostPosted: Tue Apr 20, 2010 8:04 pm 
Newbie

Joined: Mon Apr 19, 2010 8:16 pm
Posts: 2
Here is a block of code in the Java Persistence with Hibernate book by Christian and Gavin, it's from the conversation chapter,

Code:
Session session = getSessionFactory().openSession();
session.setFlushMode(FlushMode.MANUAL);
// First step in the conversation
session.beginTransaction();
Item item = (Item) session.get(Item.class, new Long(123) );
session.getTransaction().commit();
// Second step in the conversation
session.beginTransaction();
Item newItem = new Item();
Long newId = (Long) session.save(newItem); // Triggers INSERT!
session.getTransaction().commit();
// Roll back the conversation!
session.close();



I am confused that why the first step and second step need to be wrapped into two separate transactions? Since the flushmode is set manual here, no operations (suppose we ignore the insert here) will hit the database anyway. So what exactly happens when session.getTransaction().commit() is called when session's flush mode is set MANUAL?

thanks


Top
 Profile  
 
 Post subject: Re: transaction.commit() when session's flush mode is manual?
PostPosted: Wed Apr 21, 2010 2:35 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
So what exactly happens when session.getTransaction().commit() is called when session's flush mode is set MANUAL?


session.getTransaction().commit() propagates a commit on the database.
The database must commit its transaction although there were no DML SQL statements at all, which means committing an empty transaction.

This kind of conversation in my eyes is a trick to span a conversation over more transactions,
getting some ACID aspects valid for the whole conversation.

What's a little confusing in this chapter, is that the reader is asking himself:
why do call commit on a empty transaction (nothing flushed) at all?

The answer is probably, that in a typical EJB conversation you don't have direct control over commit at all
because there you will use declarative transaction demarcation instead to programmatic transaction demarcation.
It is in this context which the delay of the flush until the last step of the conversation makes sense.
This is at least what I understood from this chapter.


Top
 Profile  
 
 Post subject: Re: transaction.commit() when session's flush mode is manual?
PostPosted: Fri Apr 23, 2010 5:56 pm 
Newbie

Joined: Mon Apr 19, 2010 8:16 pm
Posts: 2
Thanks for your clarification!


Top
 Profile  
 
 Post subject: Re: transaction.commit() when session's flush mode is manual?
PostPosted: Sat Nov 06, 2010 4:26 pm 
Newbie

Joined: Sat Nov 06, 2010 4:21 pm
Posts: 3
In particular, it is presumed that there is some sort of user interaction (with a potentially unbounded human reaction time) between the two steps in this conversation, and that's why they can't be in the same transaction.


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