2012-04-18 3 views
-1

나는이 주제에 관한 많은 기사를 읽었으며 여전히 중요하고 기본적인 질문을 가지고있다. SQL 용으로 사용하지 않는 트랜잭션의 용도는 무엇입니까? wcf에서 어떻게 도움이됩니까?거래의 목적

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

그들이 경우 코드가 실패 복원하려고 무엇을 적 롤백로 아래의이 코드의 목적은 무엇인가 :이 문서에서는 간단한 트랜잭션을 사용하는 예를 보여 ?

void RootMethod() 
{ 
    using(TransactionScope scope = new TransactionScope()) 
    { 
      /* Perform transactional work here */ 
      SomeMethod(); 
      scope.Complete(); 
    } 
} 

void SomeMethod() 
{ 
    using(TransactionScope scope = new TransactionScope()) 
    { 
      /* Perform transactional work here */ 
      scope.Complete(); 
    } 
} 
+1

같은 :

는 여기에 내가 매우 분명하다 생각 마이크로 소프트의 예제 코드이다. 모든 것이 발생하거나 아무것도하지 않습니다. – cHao

+0

마지막 질문을 삭제했습니다. 동시에 한 가지 질문 만하십시오. – usr

+0

@cHao 사실이 아닙니다. 트랜잭션을 지원하는 자원 만 참여합니다. – usr

답변

2

트랜잭션을 지원하는 리소스 만 트랜잭션에 참여합니다. 일반적으로 이것은 SQL Server 일뿐입니다. 또한 메시지 큐는 트랜잭션을 지원합니다.

정상적인 변수와 같은 트랜잭션 가능 리소스를 사용하지 않으면 아무 것도 수행하지 않습니다.

0

여기 좋은 예 예에서

http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope(v=vs.90).aspx#Y500

있다 (위의 링크는 아래 코드) 자동 트랜잭션에 참여할 SQL 연결 개체 - 따라서 이러한 SQL 명령을하기 전에 커밋되지 않습니다 거래가 완료되었습니다 (scope.Complete). 이런 식으로 두 서버가 서로 다른 경우에만 두 서버에서 두 서버가 발생합니다. 예외가 있으면 둘 다 롤백됩니다.

예제는 중첩 된 범위를 설명하기 위해 해당 페이지의 컨텍스트에서만 발생하기 때문에 명확하지 않습니다. 이 예제는 뭔가 유용한 코드를 작성하는 것이 아니라 중첩 된 것으로 간주되어야하는 것을 보여줍니다. 다른 곳이다, 내가 상상 트랜잭션과 같은

// This function takes arguments for 2 connection strings and commands to create a transaction 
// involving two SQL Servers. It returns a value > 0 if the transaction is committed, 0 if the 
// transaction is rolled back. To test this code, you can connect to two different databases 
// on the same server by altering the connection string, or to another 3rd party RDBMS by 
// altering the code in the connection2 code block. 
static public int CreateTransactionScope(
    string connectString1, string connectString2, 
    string commandText1, string commandText2) 
{ 
    // Initialize the return value to zero and create a StringWriter to display results. 
    int returnValue = 0; 
    System.IO.StringWriter writer = new System.IO.StringWriter(); 

    try 
    { 
     // Create the TransactionScope to execute the commands, guaranteeing 
     // that both commands can commit or roll back as a single unit of work. 
     using (TransactionScope scope = new TransactionScope()) 
     { 
      using (SqlConnection connection1 = new SqlConnection(connectString1)) 
      { 
       // Opening the connection automatically enlists it in the 
       // TransactionScope as a lightweight transaction. 
       connection1.Open(); 

       // Create the SqlCommand object and execute the first command. 
       SqlCommand command1 = new SqlCommand(commandText1, connection1); 
       returnValue = command1.ExecuteNonQuery(); 
       writer.WriteLine("Rows to be affected by command1: {0}", returnValue); 

       // If you get here, this means that command1 succeeded. By nesting 
       // the using block for connection2 inside that of connection1, you 
       // conserve server and network resources as connection2 is opened 
       // only when there is a chance that the transaction can commit. 
       using (SqlConnection connection2 = new SqlConnection(connectString2)) 
       { 
        // The transaction is escalated to a full distributed 
        // transaction when connection2 is opened. 
        connection2.Open(); 

        // Execute the second command in the second database. 
        returnValue = 0; 
        SqlCommand command2 = new SqlCommand(commandText2, connection2); 
        returnValue = command2.ExecuteNonQuery(); 
        writer.WriteLine("Rows to be affected by command2: {0}", returnValue); 
       } 
      } 

      // The Complete method commits the transaction. If an exception has been thrown, 
      // Complete is not called and the transaction is rolled back. 
      scope.Complete(); 

     } 

    } 
    catch (TransactionAbortedException ex) 
    { 
     writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message); 
    } 
    catch (ApplicationException ex) 
    { 
     writer.WriteLine("ApplicationException Message: {0}", ex.Message); 
    } 

    // Display messages. 
    Console.WriteLine(writer.ToString()); 

    return returnValue; 
} 
+0

위의 예제에서 후자의 Connection 인 Connection2가 Connection1의 초기화 내부에서 초기화되는 이유는 무엇입니까? 그것은 중요하거나 측면 Connection1의 초기화 밖으로 할 수 있습니까? –

+0

누구나 anser에게 그것을 원하십니까? –

+0

코멘트에 바로 있습니다. connection1을 연결하고 작동시킬 수 있으면 connection2 만 열어야하기 때문에 자원을 절약 할 수 있습니다. 그렇지 않으면 어쨌든 롤백이 끝납니다. 코멘트를 읽으십시오. – Hogan