TransactionScope with SQLite Database in Database and NHibernate

I had a problem when a transaction is not rolled back when using TransactionScope.

We use NHibernate with an in-memory SQLite database, so this limits only one db connection throughout the life of the application (in this case, some unit tests).

using (var ts = new TransactionScope(TransactionScopeOption.Required, 
                                     TimeSpan.Zero))
{
    using (var transaction = _repository.BeginTransaction())
    {
        _repository.Save(entity);
        transaction.Commit();
    }
    // ts.Complete(); <- commented Complete call still commits transaction
}

Even if I delete the NHibernate internal nested transaction, so the code is just below, the transaction is still executing.

using (var ts = new TransactionScope(TransactionScopeOption.Required, 
                                     TimeSpan.Zero))
{       
    _repository.Save(entity);        
} // no Complete(), but the transaction still commits 

Is a recently opened SQLite connection expected inside a block TransactionScopeto enlist it in a transaction?

Again, I cannot provide it with a new connection because this will clear the database.

Using NHibernate 3.0 and SQLite 1.0.66.0, both latest versions at the time of writing.

:, transaction.Rollback() NHibernate ITransaction, , TransactionScope, , , .

+3
1

, . TransactionScope, .

: http://msdn.microsoft.com/en-us/library/aa720033(v=vs.71).aspx

:

.BeginTransaction() , , .

, :

    /// <summary>
    /// Begins an explicit transaction.
    /// </summary>
    /// <returns></returns>
    public ITransaction BeginTransaction()
    {
        if (System.Transactions.Transaction.Current != null)
        {
            ((DbConnection) Session.Connection).EnlistTransaction(System.Transactions.Transaction.Current);
        }
        return Session.BeginTransaction();
    }

:

  using (var ts = new TransactionScope(TransactionScopeOption.Required, TimeSpan.Zero))
  using (var transaction = repository.BeginTransaction())
  {
       repository.Save(entity);
       transaction.Commit(); // nhibernate transaction is commited
        // ts.Complete(); // TransactionScope is not commited
  } // transaction is correctly rolled back now
+1

All Articles