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