Difference Between Transaction and TransactionScope

有些话、适合烂在心里 提交于 2020-01-22 07:15:26

问题


I am developing an application which communicates with an SQL Server 2005 database to execute some stored procedures. My client demands that all transactions be managed on the C# side and not by SQL Server, and so I am using System.Transactions.TransactionScope when accessing the database. However, I have just seen the System.Transactions.Transaction datatype, and I am confused... What are the main pros/cons of each type? Which one should I use?

Please note that I must also use Enterprise Library's Data Access Application Block.


回答1:


From msdn :

The TransactionScope class provides a simple way to mark a block of code as participating in a transaction, without requiring you to interact with the transaction itself. A transaction scope can select and manage the ambient transaction automatically. Due to its ease of use and efficiency, it is recommended that you use the TransactionScope class when developing a transaction application. When you instantiate TransactionScope, the transaction manager determines which transaction to participate in. Once determined, the scope always participates in that transaction. The decision is based on two factors: whether an ambient transaction is present and the value of the TransactionScopeOption parameter in the constructor. The ambient transaction is the transaction within which your code executes. You can obtain a reference to the ambient transaction by calling the static Current property of the Transaction class.

You can read more about that here :

http://msdn.microsoft.com/en-us/library/ms172152(v=vs.90).aspx

http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope(v=vs.90).aspx

Great (a bit old) article about transaction in .NET 2.0

http://msdn.microsoft.com/en-us/library/ms973865.aspx



来源:https://stackoverflow.com/questions/6493173/difference-between-transaction-and-transactionscope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!