여기 좋은 예 예에서
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;
}
같은 :
는 여기에 내가 매우 분명하다 생각 마이크로 소프트의 예제 코드이다. 모든 것이 발생하거나 아무것도하지 않습니다. – cHao마지막 질문을 삭제했습니다. 동시에 한 가지 질문 만하십시오. – usr
@cHao 사실이 아닙니다. 트랜잭션을 지원하는 자원 만 참여합니다. – usr