static async Task <int> DoRollbackAllAndDisposeTestAsync(ISqlConnectionTransactionController controller, int tranCount, int messageId)
        {
            var message = Guid.NewGuid().ToString();

            using (var tran = controller.BeginTransaction())
            {
                tran.Status.Should().Be(SqlTransactionStatus.Opened);
                controller.TransactionCount.Should().Be(tranCount + 1);
                (await AddMessageAsync(controller, message)).Should().Be(++messageId);
                (await ReadMessageAsync(controller, messageId)).Should().Be(message);
                tran.RollbackAll();
                controller.TransactionCount.Should().Be(0);
                tran.Status.Should().Be(SqlTransactionStatus.Rollbacked);
            }
            (await ReadMessageAsync(controller, 2)).Should().BeNull();
            message = Guid.NewGuid().ToString();
            using (var tran = controller.BeginTransaction())
            {
                tran.Status.Should().Be(SqlTransactionStatus.Opened);
                controller.TransactionCount.Should().Be(1);
                (await AddMessageAsync(controller, message)).Should().Be(++messageId);
                (await ReadMessageAsync(controller, messageId)).Should().Be(message);
            }
            controller.TransactionCount.Should().Be(0);
            (await ReadMessageAsync(controller, messageId)).Should().BeNull();
            return(messageId);
        }
Exemple #2
0
 /// <summary>
 /// Calls the database and retrieves the current <see cref="IsolationLevel"/> if the
 /// connection is opened.
 /// Returns <see cref="IsolationLevel.Unspecified"/> if the database is not opened.
 /// </summary>
 /// <param name="this">This transaction aware controller.</param>
 /// <returns>The isolation level.</returns>
 public static IsolationLevel GetCurrentIsolationLevel(this ISqlConnectionTransactionController @this)
 {
     if (@this.Connection.State != ConnectionState.Open)
     {
         return(IsolationLevel.Unspecified);
     }
     using (var cmd = new SqlCommand(_getIsolationLevel))
     {
         cmd.Transaction = @this.Transaction;
         cmd.Connection  = @this.Connection;
         object o = cmd.ExecuteScalar();
         return(Parse(o == DBNull.Value ? null : (string)o));
     }
 }
Exemple #3
0
        /// <summary>
        /// Calls the database and retrieves the current <see cref="IsolationLevel"/> if the
        /// connection is opened.
        /// Returns <see cref="IsolationLevel.Unspecified"/> if the database is not opened.
        /// </summary>
        /// <param name="this">This connection controller.</param>
        /// <param name="cancel">Optional cancellation token.</param>
        /// <returns>The isolation level.</returns>
        public static async Task <IsolationLevel> GetCurrentIsolationLevelAsync(this ISqlConnectionTransactionController @this, CancellationToken cancel = default(CancellationToken))
        {
            if (@this.Connection.State != ConnectionState.Open)
            {
                return(IsolationLevel.Unspecified);
            }
            using (var cmd = new SqlCommand(_getIsolationLevel))
            {
                cmd.Transaction = @this.Transaction;
                cmd.Connection  = @this.Connection;
                object o = await cmd.ExecuteScalarAsync();

                return(Parse(o == DBNull.Value ? null : (string)o));
            }
        }
        static async Task <int> DoCommitTestAsync(ISqlConnectionTransactionController controller, int tranCount, int messageId)
        {
            var message = Guid.NewGuid().ToString();

            using (var tran = controller.BeginTransaction())
            {
                tran.Status.Should().Be(SqlTransactionStatus.Opened);
                controller.TransactionCount.Should().Be(tranCount + 1);
                (await AddMessageAsync(controller, message)).Should().Be(++messageId);
                (await ReadMessageAsync(controller, messageId)).Should().Be(message);
                tran.Commit();
                controller.TransactionCount.Should().Be(tranCount);
                tran.Status.Should().Be(SqlTransactionStatus.Committed);
            }
            (await ReadMessageAsync(controller, messageId)).Should().Be(message);
            return(messageId);
        }
        static int DoCommitTest(ISqlConnectionTransactionController controller, int tranCount, int messageId)
        {
            var message = Guid.NewGuid().ToString();

            using (var tran = controller.BeginTransaction())
            {
                tran.Status.Should().Be(SqlTransactionStatus.Opened);
                controller.TransactionCount.Should().Be(tranCount + 1);
                AddMessage(controller, message).Should().Be(++messageId);
                ReadMessage(controller, messageId).Should().Be(message);
                tran.Commit();
                controller.TransactionCount.Should().Be(tranCount);
                tran.Status.Should().Be(SqlTransactionStatus.Committed);
            }
            ReadMessage(controller, messageId).Should().Be(message);
            return(messageId);
        }
Exemple #6
0
 static bool SetTransactionLevel(ISqlConnectionTransactionController controller, IsolationLevel isolationLevel)
 {
     using (var cmd = new SqlCommand($"SET TRANSACTION ISOLATION LEVEL {isolationLevel.ToSqlString()};"))
     {
         cmd.Transaction = controller.Transaction;
         cmd.Connection  = controller.Connection;
         try
         {
             cmd.ExecuteNonQuery();
             return(true);
         }
         catch (Exception ex)
         {
             controller.SqlCallContext.Monitor.Error($"Failed to set isolation level {isolationLevel}.", ex);
             return(false);
         }
     }
 }