-->
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.  [ 9 posts ] 
Author Message
 Post subject: Possible bug?
PostPosted: Fri Nov 21, 2003 5:03 am 
Beginner
Beginner

Joined: Fri Nov 21, 2003 4:50 am
Posts: 23
Hi,

I experimented a bit with hibernate and have some strange behaviour on rollback:

Transaction hibernateTransaction = hibernateSession.beginTransaction();

Employee employee = new Employee();
// initialize employee here

hibernateSession.save(employee);
hibernateTransaction.commit();

Iterator iter = hibernateSession.iterate("from " + Employee.class.getName()");
while ( iter.hasNext() ) {
System.out.println("found after commit" + iter.next());
}


hibernateSession.delete(employee);

//hibernateSession.flush();

hibernateTransaction.rollback();

iter = hibernateSession.iterate("from " + Employee.class.getName());
while ( iter.hasNext() ) {
System.out.println("found after rollback " + iter.next());
}


A employee is saved and commited. A delete is done, but rollbacked. However, the created employee is not found in the query. If I uncomment the flush, it works as expected.

I use hibernate 2.0.3

Armin


Top
 Profile  
 
 Post subject: Possible bug more infos
PostPosted: Fri Nov 21, 2003 5:08 am 
Beginner
Beginner

Joined: Fri Nov 21, 2003 4:50 am
Posts: 23
If I add a commit to the the end, the employee is deleted in the database also.

Armin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 5:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you don't flush the session before the rollback(), the deleting is flushed to the database when you call iterate() - ie. in the next txn.

This is quite expected.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 5:20 am 
Beginner
Beginner

Joined: Fri Nov 21, 2003 4:50 am
Posts: 23
[quote="gavin"]If you don't flush the session before the rollback(), the deleting is flushed to the database when you call iterate() - ie. in the next txn.

This is quite expected.[/quote]

But what is with auto flush? My idea was, that set flush mode to auto flush exactly did the flush before commit and rolback automatically.

Armin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 5:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
nope. autoflush flushes before commit, not before rollback


Top
 Profile  
 
 Post subject: Possible Bug/Flush problem
PostPosted: Fri Nov 21, 2003 5:31 am 
Beginner
Beginner

Joined: Fri Nov 21, 2003 4:50 am
Posts: 23
The test code slightly modified:

Transaction hibernateTransaction = hibernateSession.beginTransaction();

Employee employee = new Employee();
// initialize employee here

hibernateSession.save(employee);
hibernateTransaction.commit();

Iterator iter = hibernateSession.iterate("from " + Employee.class.getName()");
while ( iter.hasNext() ) {
System.out.println("found after commit" + iter.next());
}


hibernateSession.delete(employee);

//hibernateSession.flush();

hibernateTransaction.rollback();

iter = hibernateSession.iterate("from " + Employee.class.getName());
while ( iter.hasNext() ) {
System.out.println("found after rollback " + iter.next());
}

hibernateSession.flush();

hibernateTransaction.commit();

iter = hibernateSession.iterate("from " + Employee.class.getName());
while ( iter.hasNext() ) {
System.out.println("found after commit " + iter.next());
}



In words: save and commit a employee. Delete the employee, rolllback the transaction, flush and the commit!

The result is, that the employee is deleted in database and not found with the query!

My understanding for this code is that the saved employee should not be deleted. Is this understanding incorrect?


Armin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 5:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes your understanding is completely incorrect.

If you roll back a transaction, you should discard the Hibernate session.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 5:32 am 
Beginner
Beginner

Joined: Fri Nov 21, 2003 4:50 am
Posts: 23
[quote="gavin"]nope. autoflush flushes before commit, not before rollback[/quote]

Ok I found that in the documention, thanx, but I posted a slightly modified code example, I don't understand

Armin


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 6:23 am 
Beginner
Beginner

Joined: Fri Nov 21, 2003 4:50 am
Posts: 23
[quote="gavin"]Yes your understanding is completely incorrect.

If you roll back a transaction, you should discard the Hibernate session.[/quote]

Ok, but then a problem occurs. I change the objects loaded from the now closed session and want to save them in a new session, I got an exception

net.sf.hibernate.HibernateException: Another object was associated with this id

Must I reload all the objects from the new session? But this breaks my long transaction semantics!

Armin


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