Example #1
0
        /// <summary>
        /// Rolls back the <see cref="ITransaction"/> by calling the method <c>Rollback</c>
        /// on the underlying <see cref="DbTransaction"/>.
        /// </summary>
        /// <exception cref="TransactionException">
        /// Thrown if there is any exception while trying to call <c>Rollback()</c> on
        /// the underlying <see cref="DbTransaction"/>.
        /// </exception>
        public void Rollback()
        {
            using (SessionIdLoggingContext.CreateOrNull(sessionId))
            {
                CheckNotDisposed();
                CheckBegun();
                CheckNotZombied();

                log.Debug("Rollback");

                if (!commitFailed)
                {
                    try
                    {
                        trans.Rollback();
                        log.Debug("DbTransaction RolledBack");
                        rolledBack = true;
                        Dispose();
                    }
                    catch (HibernateException e)
                    {
                        log.Error(e, "Rollback failed");
                        // Don't wrap HibernateExceptions
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error(e, "Rollback failed");
                        throw new TransactionException("Rollback failed with SQL Exception", e);
                    }
                    finally
                    {
                        AfterTransactionCompletion(false);
                        CloseIfRequired();
                    }
                }
            }
        }
        /// <summary>
        /// Takes care of freeing the managed and unmanaged resources that
        /// this class is responsible for.
        /// </summary>
        /// <param name="isDisposing">Indicates if this AdoTransaction is being Disposed of or Finalized.</param>
        /// <remarks>
        /// If this AdoTransaction is being Finalized (<c>isDisposing==false</c>) then make sure not
        /// to call any methods that could potentially bring this AdoTransaction back to life.
        /// </remarks>
        protected virtual void Dispose(bool isDisposing)
        {
            using (SessionIdLoggingContext.CreateOrNull(sessionId))
            {
                if (_isAlreadyDisposed)
                {
                    // don't dispose of multiple times.
                    return;
                }
                _isAlreadyDisposed = true;

                // free managed resources that are being managed by the AdoTransaction if we
                // know this call came through Dispose()
                if (isDisposing)
                {
                    if (trans != null)
                    {
                        trans.Dispose();
                        trans = null;
                        log.Debug("DbTransaction disposed.");
                    }

                    if (IsActive)
                    {
                        // Assume we are rolled back
                        rolledBack = true;
                        if (session != null)
                        {
                            AfterTransactionCompletion(false);
                        }
                    }
                    // nothing for Finalizer to do - so tell the GC to ignore it
                    GC.SuppressFinalize(this);
                }

                // free unmanaged resources here
            }
        }
Example #3
0
        public async Task EnabledAsync()
        {
            var guid = Guid.NewGuid();

            using (SessionIdLoggingContext.CreateOrNull(guid))
            {
                Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid));
                await Task.Delay(1).ConfigureAwait(false);

                Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid));

                var guid2 = Guid.NewGuid();
                using (SessionIdLoggingContext.CreateOrNull(guid2))
                {
                    Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid2));
                    await Task.Delay(1).ConfigureAwait(false);

                    Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid2));
                }
                Assert.That(SessionIdLoggingContext.SessionId, Is.EqualTo(guid));
            }
            Assert.That(SessionIdLoggingContext.SessionId, Is.Null);
        }
Example #4
0
        /// <summary>
        /// Takes care of freeing the managed and unmanaged resources that
        /// this class is responsible for.
        /// </summary>
        /// <param name="isDisposing">Indicates if this AdoTransaction is being Disposed of or Finalized.</param>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
        /// <remarks>
        /// If this AdoTransaction is being Finalized (<c>isDisposing==false</c>) then make sure not
        /// to call any methods that could potentially bring this AdoTransaction back to life.
        /// </remarks>
        protected virtual async Task DisposeAsync(bool isDisposing, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (SessionIdLoggingContext.CreateOrNull(sessionId))
            {
                if (_isAlreadyDisposed)
                {
                    // don't dispose of multiple times.
                    return;
                }

                // free managed resources that are being managed by the AdoTransaction if we
                // know this call came through Dispose()
                if (isDisposing)
                {
                    if (trans != null)
                    {
                        trans.Dispose();
                        trans = null;
                        log.Debug("DbTransaction disposed.");
                    }

                    if (IsActive && session != null)
                    {
                        // Assume we are rolled back
                        await(AfterTransactionCompletionAsync(false, cancellationToken)).ConfigureAwait(false);
                    }
                }

                // free unmanaged resources here

                _isAlreadyDisposed = true;
                // nothing for Finalizer to do - so tell the GC to ignore it
                GC.SuppressFinalize(this);
            }
        }