Skip to content

Using System.Transaction with IStatelessSession doesn't always flush batches to database #2750

Closed
@jklawrence

Description

@jklawrence

I'm having two issues with using System.Transaction and IStatelessSession together.

First, it doesn't work at all with the default UseConnectionOnSystemTransactionPrepare value of true. Committing a transaction like:

using (var transaction = new TransactionScope())
{
	using (var session = sessionFactory.OpenStatelessSession())
	{
		// save a change here
	}
	transaction.Complete();
}

always results in this exception:

System.Transactions.TransactionAbortedException: The transaction has aborted. ---> System.InvalidOperationException: Batcher still has opened ressources at time of processing from system transaction.
   at NHibernate.AdoNet.ConnectionManager.BeginProcessingFromSystemTransaction(Boolean allowConnectionUsage)
   at NHibernate.Transaction.AdoNetWithSystemTransactionFactory.SystemTransactionContext.Prepare(PreparingEnlistment preparingEnlistment)
   --- End of inner exception stack trace ---
   at System.Transactions.TransactionStateAborted.EndCommit(InternalTransaction tx)
   at System.Transactions.CommittableTransaction.Commit()
   at System.Transactions.TransactionScope.InternalDispose()
   at System.Transactions.TransactionScope.Dispose()
   at StatelessBatchFlush.Program.ValidateBatching(ISessionFactory sessionFactory) in C:\Projects\NHibernate-Stateless\Program.cs:line 68

Setting UseConnectionOnSystemTransactionPrepare=false does cause this exception to go away, but with that set the last batch of items does not get flushed to the database. IStatelessSession does not expose a Flush() method, but we've found that calling session.GetSessionImplementation().Flush() before completing the transaction is a workaround for this.

I've created a small console app to demo the problems I'm having here: https://github.com/jklawrence/NHibernate-Stateless

These same scenarios work fine when using an NHibernate transaction instead of System.Transaction.

NHibernate version: 5.3.8

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions