-->
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.  [ 1 post ] 
Author Message
 Post subject: flush without commit permanently updates DB?
PostPosted: Mon Jun 29, 2009 10:29 am 
Newbie

Joined: Mon Jun 29, 2009 7:52 am
Posts: 1
I know (based on various forum discussions) that you've been asked questions along this vein multiple times, but I'm pretty stumped and maybe you can help:

The scenario I'm struggling with is pretty simple; I need to insert records into two different tables within the same transaction. The problem is that, for some reason, when an exception is thrown because of a failure to insert into the second table and I roll back the transaction, the record remains in the first table. Here's the code snippet:

try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.persist(test1);
session.persist(test2);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
} finally {
session.close();
}

In trying to figure out the problem, I even changed the FlushMode to COMMIT and explicitly did a session.flush() prior to the tx.commit(). The result was that even though tx.commit() is never executed, because an exception is thrown when session.flush() is executed, the test1 record remains in the table!? Again, here's the code:

try {
session = sessionFactory.openSession();
session.setFlushMode(FlushMode.MANUAL);
tx = session.beginTransaction();
session.persist(test1);
session.persist(test2);
session.flush();
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
} finally {
session.close();
}

Any light you can shed on this?

Thanks!


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

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.