Is TransactionScope implicitly applied until explicitly completed?

Consider the following methods.

DoA()
{
  using (TransactionScope scope = new TransactionScope)
  {
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
      connection.Open();
      SqlCommand command = new SqlCommand(query, connection);
      command.ExecuteNonReader();

      DoB();    

      scope.Complete();
    }
  }
}

DoB()
{
  using (TransactionScope scope = new TransactionScope)
  {
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
      connection.Open();
      SqlCommand command = new SqlCommand(query, connection);
      command.ExecuteNonReader();

      DoC();

      scope.Complete();
    }
  }
}

DoC()
{
  using (SqlConnection connection = new SqlConnection(connectionString))
  {
    connection.Open();
    SqlCommand command = new SqlCommand(query, connection);
    command.ExecuteNonReader();
  }
}

If we call DoA(), do the subsequent interactions in DoB()and DoC()in the context of a transaction DoA(), how does this apply to SQL Server? Is DoC () executed in the context of transactions DoA()and DoB()?

(Or am I really misunderstanding something?)

+5
source share
2 answers

. ( RequiresNew), . , , , Complete, .

DoC ; .

, , Requires, RequiresNew, Suppress.

, , . .

+4

:

, - SQL:

BEGIN TRANSACTION A

  -- do A work

  -- B does NOT create a new transaction

    -- do B work

    -- do C work

COMMIT TRANSACTION A

new TransactionScope(TransactionScopeOption.RequiresNew) DoB() :

BEGIN TRANSACTION A

  -- do A work

  BEING TRANSACTION B

    -- do B work

    -- do C work

  COMMIT TRANSACTION B
COMMIT TRANSACTION A
+2

All Articles