I am unable to get the below update to execute in the context of nhibernate.
using(ITransaction transaction = session.BeginTransaction())
{
// FIRST I'm GETTING A LIST OF ITEMS IN A MANNER LIKE THIS
var itemsToDelete = session.QueryOver<Item>()
.Where(i => i.ReferenceObject.Id == otherIdValue)
.List<Item>();
// THEN I"M LOOPING THROUGH THEM
for(itemToDelete in itemsToDelete)
{
session.Delete(itemToDelete);
using (iDB2Command command = (iDB2Command)_session.Connection.CreateCommand())
{
command.CommandText = "update TABLE_NAME set sequence = (sequence - 1) where id = @someId and sequence > @sequenceNumberDeleted";
command.DeriveParameters();
command.Parameters["@someId"].Value = idValue;
command.Parameters["@sequenceNumberDeleted"].Value = itemToDelete.Sequence;
}
}
transaction.commit()
}
The problem seems to be with the sequence = (sequence – 1). Everytime the routine is called NHibernate is throwing an “unexpected row count” exception. While researching most articles I found related to this exception were caused by a trigger on the table updating other rows. In this case there aren’t any triggers on the table. Additionally if I replace sequence = 5 or some other constant the update statement executes without any problems.
DATE TIME [10] ERROR App.Controllers.AController - Unexpected row count: 2; expected: 1
DATE TIME [10] ERROR App.Controllers.AController - at NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, IDbCommand statement)
at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Int32 j, Object obj, SqlCommandInfo sql, ISessionImplementor session, Object[] loadedState)
at NHibernate.Persister.Entity.AbstractEntityPersister.Delete(Object id, Object version, Object obj, ISessionImplementor session)
at NHibernate.Action.EntityDeleteAction.Execute()
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
at NHibernate.Engine.ActionQueue.ExecuteActions()
at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
at NHibernate.Impl.SessionImpl.Flush()
at NHibernate.Transaction.AdoTransaction.Commit()
at App.Controllers.AController.AMethod(Int32[] otherIdValues, Int32 someIdValue, String someReferenceValue) in <path>\App\Controllers\AController.cs:line 117
Can someone help point me in the right direction?
EDIT:
Diego is quite right. This is what I get for rushing.
The actual call to this code is inside a transaction and then I’m calling commit on the transaction.
I have since found the cause of the issue. The actual statements were not being generated sequentially as I thought they would be. Instead the update command was executing prior to the delete command and thus the delete command was throwing the unexpected row count exception, not the update command as I previously thought.