分布式事务TransactionScope小结[转]
以下是分布式事务的所有情况的例子了,包含了事务套事务,事务套存储过程事务,经过测试,TransactionScope对于分布式事务的各种情况支持的很好。
使用分布式事务注意如下几点:
1:确保参与事务的machine开启了分布式事务支持;
2:如果machine开启了防火墙,需要设置msdtc进程为例外;
3:参与事务的machine不能跨域(如果跨域,目前微软还没有确切的解决方案);
4:多数据库时才使用分布式事务,如果是同一个数据库,最好使用SqlTransaction.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
namespace ConsoleApplication16
{
class Program
{
static void Main(string[] args)
{
TestTransaction test = new TestTransaction();
test.Test1();
test.Test2();
test.Test3();
Console.ReadLine();
}
}
public class TestTransaction
{
public void Test1()
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
ExecuteQuery1("insert into user1 (username) values ('user1')");
ExecuteQuery1("insert into user2 (username) values ('user2')");
ExecuteQuery2("insert into user1 (username) values ('user1')");
ExecuteQuery2("insert into user2 (username) values ('user2')");
scope.Complete();
}
}
public void Test2()
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
ExecuteQuery1(@"begin tran insert into user2 (username) values ('tran1') insert into user2 (username) values ('tran2') commit tran");
ExecuteQuery2(@"begin tran insert into user2 (username) values ('tran1') insert into user2 (username) values ('tran2') commit tran");
scope.Complete();
}
}
public void Test3()
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
Test1();
Test2();
scope.Complete();
}
}
private void ExecuteQuery1(string query)
{
string connectionString = "Data Source=wrf;Initial Catalog=test1;Integrated Security=True;";
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand com = new SqlCommand(query, con))
{
con.Open();
com.ExecuteNonQuery();
}
}
}
private void ExecuteQuery2(string query)
{
string connectionString = "Data Source=192.168.1.113;Initial Catalog=test1;Persist Security Info=True;User ID=sa;Password=123;";
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand com = new SqlCommand(query, con))
{
con.Open();
com.ExecuteNonQuery();
}
}
}
}
}
使用分布式事务注意如下几点:
1:确保参与事务的machine开启了分布式事务支持;
2:如果machine开启了防火墙,需要设置msdtc进程为例外;
3:参与事务的machine不能跨域(如果跨域,目前微软还没有确切的解决方案);
4:多数据库时才使用分布式事务,如果是同一个数据库,最好使用SqlTransaction.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
namespace ConsoleApplication16
{
class Program
{
static void Main(string[] args)
{
TestTransaction test = new TestTransaction();
test.Test1();
test.Test2();
test.Test3();
Console.ReadLine();
}
}
public class TestTransaction
{
public void Test1()
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
ExecuteQuery1("insert into user1 (username) values ('user1')");
ExecuteQuery1("insert into user2 (username) values ('user2')");
ExecuteQuery2("insert into user1 (username) values ('user1')");
ExecuteQuery2("insert into user2 (username) values ('user2')");
scope.Complete();
}
}
public void Test2()
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
ExecuteQuery1(@"begin tran insert into user2 (username) values ('tran1') insert into user2 (username) values ('tran2') commit tran");
ExecuteQuery2(@"begin tran insert into user2 (username) values ('tran1') insert into user2 (username) values ('tran2') commit tran");
scope.Complete();
}
}
public void Test3()
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
Test1();
Test2();
scope.Complete();
}
}
private void ExecuteQuery1(string query)
{
string connectionString = "Data Source=wrf;Initial Catalog=test1;Integrated Security=True;";
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand com = new SqlCommand(query, con))
{
con.Open();
com.ExecuteNonQuery();
}
}
}
private void ExecuteQuery2(string query)
{
string connectionString = "Data Source=192.168.1.113;Initial Catalog=test1;Persist Security Info=True;User ID=sa;Password=123;";
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand com = new SqlCommand(query, con))
{
con.Open();
com.ExecuteNonQuery();
}
}
}
}
}