数据访问应用程序块和事务范围将单独的数据库对象使用相同的连接
我将 System.Transactions 和 Transaction 范围与 Enterprise Library 数据访问应用程序块结合使用进行事务处理。
在某些情况下,我在事务中使用数据库类的单独实例。 但连接是到同一个数据库。
我想知道在这种情况下应用程序块是否会重用相同的连接而不跨越分布式事务?
I'm using System.Transactions and Transaction scope for my transaction handling in conjunction with the Enterprise Library Data Access Application Block.
In some cases I'm using separate instances of the Database class within the transaction. The connection is to the same database though.
I'm wondering if the application block will reuse the same connection and not span a distributed transaction in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简而言之,MSDN 给出了您的答案:
“企业库,在另一方面,通常为每个请求打开和关闭一个连接,这种方法与 TransactionScope 类的工作方式不兼容,如果有多个连接,TransactionScope 类会认为该事务是分布式事务,并且具有显着的性能。与本地事务相比的资源开销。” (MSDN)
并且
“为了避免这种情况,Database 类方法(例如 ExecuteDataSet)会识别 TransactionScope 实例何时处于活动状态,并在此事务中登记数据库调用。如果事务当前由于使用 TransactionScope 实例而处于活动状态,则数据库类方法使用单个连接。” (MSDN)
你没有提到你正在使用什么数据库。 在我使用的 Oracle 10g 客户端中,如果您使用 TransactionScope,您将始终拥有分布式事务。 看起来这个问题现已在后续版本中得到解决。
您可以通过查看交易统计。
In a nutshell MSDN has your answer:
"Enterprise Library, on the other hand, normally opens and closes a connection for each request. This approach is incompatible with the way the TransactionScope class works. If there are multiple connections, the TransactionScope class considers the transaction to be a distributed transaction. Distributed transactions have a significant performance and resource overhead compared with a local transaction." (MSDN)
And
"To avoid this, the Database class methods, such as ExecuteDataSet, recognize when a TransactionScope instance is active and they enlist database calls in this transaction. If a transaction is currently active as a result of using a TransactionScope instance, the Database class methods use a single connection." (MSDN)
You don't mention what database you are using. In the Oracle 10g client I was using, it was the case that if you used TransactionScope you would always have a distributed transaction. It looks like this issue is now addressed in later releases.
You can check the answer by looking under Transaction Statistics.
TransactionScope
是一个非常奇怪的野兽。 根据我的经验,一旦另一个连接(甚至到同一个数据库)打开,数据库事务就会升级为分布式事务。TransactionScope
is a very strange beast. To my experience a DB transaction is promoted to a distributed one as soon as another connection (even to the same DB) gets open.