TransactionScope 涉及进入范围之前创建的 DbTransaction
我们曾经对一些遗留业务对象使用DAAB,现在我们'我们决定将 TransactionScope
用于一些新的业务对象。现在我们必须在旧业务对象代码中调用新业务对象。当在其中调用新业务对象时,旧业务对象代码将如下所示:
Database db = DatabaseFactory.CreateDatabase();
// create connection instance
DbConnection dbConnection = db.CreateConnection();
dbConnection.Open(); // open connection
// create transaction instance & start the transaction
DbTransaction dbTransaction = dbConnection.BeginTransaction();
// do some old business object insert/update
// now call the new business object
newBO.Update();
// commit transaction
dbTransaction.Commit();
新业务对象中的代码如下:
using (TransactionScope scope = new TransactionScope())
{
// do some update/insert
scope.Complete();
}
问题是,这行得通吗?
我目前的信念是,它将基于:
- 此 MSDN 文章 说,如果事务是在范围之外创建的,事务的创建者仍然负责提交/回滚它 - 这基本上是我的情况,它会起作用如果这是真的的话就完美了。
- 我完成的 TransactionScope 的部分跟踪还表明,在处置 TransactionScope 时不一定会提交提交,但我无法完全完成跟踪,因此我不能 100% 确定我的跟踪是否准确,
但是,有几篇文章关于TransactionScope,包括这篇关于SO的文章,它没有提到这种情况,只是表示事务将被回滚,而不指定事务是否由 TransactionScope 本身创建。
所以我的问题是:根据您的经验,是这种情况吗?
a) 在 TransactionScope 之外创建的事务在处理范围时提交
或
b) 在 TransactionScope 之外创建的事务在处理时不提交,并且创建者必须负责回滚和处理犯罪?
We used to use DAAB for some legacy business objects and now we've decided to use TransactionScope
for some new business objects. Now we have to call the new business object within the old business object code. The old business object code would look like this when the new business object is called within it:
Database db = DatabaseFactory.CreateDatabase();
// create connection instance
DbConnection dbConnection = db.CreateConnection();
dbConnection.Open(); // open connection
// create transaction instance & start the transaction
DbTransaction dbTransaction = dbConnection.BeginTransaction();
// do some old business object insert/update
// now call the new business object
newBO.Update();
// commit transaction
dbTransaction.Commit();
The new business object has code in it as follows:
using (TransactionScope scope = new TransactionScope())
{
// do some update/insert
scope.Complete();
}
The question is, is this going to work?
My current belief is that it will work based on:
- this MSDN article which says that if the transaction was created outside the scope, the creator of the transaction is still responsible for commit/rolling it back - which is my case basically, it will work perfectly if this is true.
- a partial trace of the TransactionScope I complete also indicates that a commit is not necessarily committed on dispose of TransactionScope but I couldn't completely finish the trace so I'm not 100% sure if my trace was accurate
However, there are a few articles on TransactionScope including this post on SO which doesn't mention this scenario and just say that the transaction(s) will be rolled back without specifying if the transaction was created by the TransactionScope itself or not.
So my question: from Your experience, which is the case?
a) transaction created outside of the TransactionScope is committed on the dispose of the scope
OR
b) transaction created outside the TransactionScope is not committed on the disposing and the creator has to take care of rollback & commit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是 B。但是,您需要记住,transactionScope 使用 MSDTC 在分布式系统级别创建事务,而 DB 事务在 DB 上创建
Answer is B. However, you need to remember, transactionScope create transaction at distributed system level using MSDTC while DB transaction created on DB