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: Transaction.Rollback doesn't rollback Session.Delete
PostPosted: Tue Apr 22, 2008 1:38 pm 
Beginner
Beginner

Joined: Thu Apr 27, 2006 5:49 am
Posts: 31
Hi, guys. Let's look at the following code:

Code:
  IList entities = service.getEntities(this.Session);
 
  foreach (Entity entity in entities)
  {
      ITransaction transaction = this.Session.BeginTransaction();
   
      try
      {
           this.Session.Delete(entity);
           transaction.Commit();   
      }
      catch (HibernateException)
      {
           transaction.Rollback();
      }
  }



It is important that each object is being deleted in a different transaction. If deleting of one of the entities throws an exception the transaction is rolled back and the next entity is tried to be deleted.

The problem in this code that actually if any of the entity couldn't be deleted then no entities after that one will be deleted. It seems session "remembers" that we tried to delete the object even if we had rolled back the transaction and new transaction had been started. And it tries to delete the problem one before deleting the next one from the list.

I've checked that after calling Session.Delete() method Session.IsDirty() returns true and after rolling back the transaction it still returns true.

Is it really an expected behavior? I mean if there was no other changes of objects associated with the session and all changes are done in the transaction shouldn't the session "forget" about every command if transaction was rolled back? Is there any way to archive such a behavior?

P.S. I am using NHibernate ver. 1.2.0.4000


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 5:40 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
If a Session throws *any* exception, regardless of whether or not you catch it the session must be disposed of. After an exception a Session is not considered to be in a usable state and a new Session should be created.

Cheers,

Symon.

_________________
Symon Rottem
http://blog.symbiotic-development.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 2:23 am 
Beginner
Beginner

Joined: Thu Apr 27, 2006 5:49 am
Posts: 31
merge_s.rottem wrote:
If a Session throws *any* exception, regardless of whether or not you catch it the session must be disposed of. After an exception a Session is not considered to be in a usable state and a new Session should be created.


Aha, so if Transaction.Commit() throws an exception the best thing I could do is to open a new session in catch block? Like this:

Code:
  IList entities = service.getEntities(this.Session);

  foreach (Entity entity in entities)
  {
      ITransaction transaction = this.Session.BeginTransaction();
   
      try
      {
           this.Session.Delete(entity);
           transaction.Commit();   
      }
      catch (HibernateException)
      {
           transaction.Rollback();
           this.Session.Close();
           this.Session = this.sessionFactory.OpenSession();
      }
  }


But what will happen with the entities that are associated with the corrupted session? After we try to delete them in another session won't it throws an exception, because the object is not associated with the session?


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.