1.上代码,使用TransactionScope 类库作事务测试
//主代码
public static void Main(string[] args)
{
using (TransactionScope ts = new TransactionScope())
{
try
{
Console.WriteLine("------------------------------------------------------------");
Console.WriteLine("我是-主线程-主事务");
Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("事务ID" + Transaction.Current.TransactionInformation.LocalIdentifier);
new IDLogTester().IdTest(1);
using (TransactionScope ts2 = new TransactionScope())
{
Console.WriteLine("------------------------------------------------------------");
Console.WriteLine("我是-主线程-子事务-继承");
Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("事务ID" + Transaction.Current.TransactionInformation.LocalIdentifier);
new IDLogTester().IdTest(2);
ts2.Complete();
}
Task.Run(() =>
{
using (TransactionScope ts3 = new TransactionScope())
{
Console.WriteLine("------------------------------------------------------------");
Console.WriteLine("我是-子线程-子事务-继承");
Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("事务ID" + Transaction.Current.TransactionInformation.LocalIdentifier);
new IDLogTester().IdTest(3);
ts3.Complete();
}
});
using (TransactionScope ts4 = new TransactionScope(TransactionScopeOption.RequiresNew))
{
Console.WriteLine("------------------------------------------------------------");
Console.WriteLine("我是-主线程-子事务-新建");
Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("事务ID" + Transaction.Current.TransactionInformation.LocalIdentifier);
new IDLogTester().IdTest(4);
ts4.Complete();
}
using (TransactionScope ts5 = new TransactionScope())
{
Console.WriteLine("------------------------------------------------------------");
Console.WriteLine("我是-主线程-子事务-继承-反射");
Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("事务ID" + Transaction.Current.TransactionInformation.LocalIdentifier);
string nspace = "ClassLibrary3";
var ns = Assembly.Load(nspace);
var obj = ns.CreateInstance($"{nspace}.IDLogTester_Other") as IDLogTester_Other;
obj.IdTest(5);
ts5.Complete();
}
Console.WriteLine("------------------------------------------------------------");
Console.WriteLine("我是-主线程-主事务-反射-子事务-继承");
Console.WriteLine("线程ID:" + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("事务ID" + Transaction.Current.TransactionInformation.LocalIdentifier);
string nspace1 = "ClassLibrary3";
var ns1 = Assembly.Load(nspace1);
var obj1 = ns1.CreateInstance($"{nspace1}.IDLogTester_Other") as IDLogTester_Other;
obj1.IdTest_ts(6);
ts.Complete();
}
catch (Exception ex)
{
ts.Complete();
Console.WriteLine(ex.Message);
}
}
//反射使用的代码
public class IDLogTester_Other: DbContext<IDLog>
{
public void IdTest(long id)
{
IDLog model = new IDLog();
model.ID = id;
model.Dt = DateTime.Now;
Db.Insertable(model).ExecuteCommand();
}
public void IdTest_ts(long id)
{
using (TransactionScope ts = new TransactionScope())
{
IDLog model = new IDLog();
model.ID = id;
model.Dt = DateTime.Now;
Db.Insertable(model).ExecuteCommand();
//throw new Exception("error");
ts.Complete();
}
}
}
2.结果和总结