两个并行的 TransactionScope,每个都有自己的连接
我试图弄清楚 TransactionScope 如何使用代表不同事务场景的示例代码来工作......
您能解释一下,当我有 2 个并发 TransactionScope,每个都有自己的连接时, date 实际发生了什么?就像这个示例代码一样:
internal class Program
{
private const int Count = 20;
private static readonly Random Random = new Random();
private static void Main(string[] args)
{
var task1 = new Task(Task1);
var task2 = new Task(Task2);
task1.Start();
task2.Start();
Console.ReadLine();
}
private static void Task1()
{
var connection = new EntityConnection("name=ModelContainer");
using (var transaction = new TransactionScope())
{
DoWork(connection);
transaction.Complete();
}
}
private static void Task2()
{
var connection = new EntityConnection("name=ModelContainer");
using (var transaction = new TransactionScope())
{
DoWork(connection);
transaction.Complete();
}
}
private static void DoWork(EntityConnection connection)
{
connection.Open();
using (var context = new ModelContainer(connection))
{
List<SyncData> list = context.SyncDataSet.ToList();
for (int i = 0; i < Count; i++)
{
list[i].Knowledge.Version = Random.Next(200);
context.SaveChanges();
}
}
}
}
I'm trying to figure out how TransactionScope works playing around with sample code representing different transactions scenarios...
Can you explain what actually happens with date when I have 2 concurrent TransactionScopes each with it's own connection? Like in this sample code:
internal class Program
{
private const int Count = 20;
private static readonly Random Random = new Random();
private static void Main(string[] args)
{
var task1 = new Task(Task1);
var task2 = new Task(Task2);
task1.Start();
task2.Start();
Console.ReadLine();
}
private static void Task1()
{
var connection = new EntityConnection("name=ModelContainer");
using (var transaction = new TransactionScope())
{
DoWork(connection);
transaction.Complete();
}
}
private static void Task2()
{
var connection = new EntityConnection("name=ModelContainer");
using (var transaction = new TransactionScope())
{
DoWork(connection);
transaction.Complete();
}
}
private static void DoWork(EntityConnection connection)
{
connection.Open();
using (var context = new ModelContainer(connection))
{
List<SyncData> list = context.SyncDataSet.ToList();
for (int i = 0; i < Count; i++)
{
list[i].Knowledge.Version = Random.Next(200);
context.SaveChanges();
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嵌套的事务范围(并且在同一线程上)成为同一事务的一部分(外部事务是完成的定义范围) - 如果需要,则升级到 DTC。在您的情况下,事务似乎不是嵌套的,因此它们是不相关的。这也意味着 LTM 而不是 DTC 是所有者,其开销较少。
因此,当然,如果它们接触相同的数据,您可能需要注意它们之间的阻塞等。特别是因为事务范围默认为可序列化隔离。
Transaction scopes that are nested (and on the same thread) become part of the same transaction (the outer transaction being the defining scope for completion) - escalating to DTC if needed. In your case the transactions don't seem nested, so they are unrelated. That should also mean that the LTM rather than DTC is the owner, which has less overhead.
As a result, of course, you may need to watch for blocking etc between these if they touch the same data. Especially since transaction scope defaults to serializable isolation.