开发者

Difference between different BeginTransaction methods.

What is the difference among the following BeginTransaction methods:

  1. SqlConnection.BeginTransaction Method

  2. DbConnection.BeginTransaction 开发者_JS百科Method

  3. DbConnection.BeginDbTransaction Method

Moreover, how they are different from TransactionScope() method in System.Transaction?


  1. SqlConnection.BeginTransaction creates a SqlTransaction, which is specific to MS SQL Server
  2. DbConnection.BeginTransaction creates a DbTransaction, which is generic, and relies on the underlying connection to create a database-specific transaction. If your DbConnection is of type SqlConnection, this will be a SqlTransaction.
  3. DbConnection.BeginDbTransaction is a protected method that you override if you're creating your own class that inherits from DbConnection.

EDIT:

These are all specific to the database connection from which they were created, which is used differently than a TransactionScope, which isn't database-dependent. I believe if you wanted to coordinate transactions between multiple connections, you have to explicitly call DbConnection.EnlistTransaction(transaction). With a TransactionScope, a connection will (depending on the database provider, at least it should) automatically enlist in the TransactionScope if one exists when the connection is opened. In WCF, a TransactionScope can also be passed across service boundaries, and can be used to commit the results of multiple service calls as a single transaction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜