Not sure this is the right place for this post, as I'm not sure this is an NHibernate issue. But, as this error is happening in a Spring/Nhibernate app involving a TxScopeTransactionManager and a PostUpdate event listener, it might be relevant.

The app is an integration platform which collaborate with legacy systems. One of them is a SQL Server 2005 database. The data access layer is using NHibernate to map domain objects to the datastore. There is also an audit requirement to track any change in tables and report them in 'history' tables. These 'history' tables cannot be changed to fit Envers audit data model. Legacy rules state that stored procedures must be used : each table has a related audit stored procedure which usually takes the primary key and the current date as parameters and copies row data in the history table.

I thought auditing through stored procedure call would be quite simple by implementing insert/update/... event listeners and configuring the SessionFactory.

Auditing inserts work quite well but I got into a lot of trouble when dealing with updates.

The error occurs on a ISession.Merge() call occurring inside a transaction. The post update listener is activated and the audit stored procedure get called but then I get the following exception :

Interception de System.InvalidOperationException
  Message=La collection a été modifiée*; l'opération d'énumération peut ne pas s'exécuter. --> Collection was modified; enumeration may not execute
       à System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
       à Spring.Transaction.Support.AbstractPlatformTransactionManager.TriggerBeforeCommit(DefaultTransactionStatus status)
       à Spring.Transaction.Support.AbstractPlatformTransactionManager.ProcessCommit(DefaultTransactionStatus status)
       à Spring.Transaction.Support.AbstractPlatformTransactionManager.Commit(ITransactionStatus transactionStatus)
       à Spring.Transaction.Interceptor.TransactionAspectSupport.CommitTransactionAfterReturning(TransactionInfo transactionInfo)
       à Spring.Transaction.Interceptor.TransactionInterceptor.Invoke(IMethodInvocation invocation)
       à Spring.Aop.Framework.AbstractMethodInvocation.Proceed()
       à Spring.Aop.Framework.DynamicProxy.AdvisedProxy.Invoke(Object proxy, Object target, Type targetType, MethodInfo targetMethod, MethodInfo proxyMethod, Object[] args, IList interceptors)
       à CompositionAopProxy_e0257a13347340278104ae8fbbaecafc.UpdateCustomer(Customer customer)
       à Console.Program.TryAuditingInsertAndUpdateOperations(ICustomerDalService customerDalService, ISessionFactory sessionFactory) dans C:\Dev\Mediation-PoC1\Console\Program.cs:ligne 223
This error seems to be related to the TxScopeTransactionManager. Using this transaction manager is quite important for us as we are dealing with several databases distributed across different machines.

A VS 2010 solution that implements the basics of the application and reproduces the error is available here.

Any clue is most welcome.

Fabrice Aupert