Difference between Implicit and Explicit Transaction

后端 未结 4 1911
长情又很酷
长情又很酷 2020-12-09 10:06

What is the difference between Implicit and Explicit transaction in Sql Server 2008?

What happens in TransactionScope background? I\'m using TransactionScope but in

相关标签:
4条回答
  • 2020-12-09 10:18

    In Explicit transaction mode, you will need to start a transaction explicitly. In Implicit transaction mode, a transaction is automatically started after each commit. So you will only have to commit.

    Since the transaction is started 'implicitly', you will not see an explicit 'BEGIN' in the logs. :)

    By default the database operates in explicit transaction mode with autocommiting transactions enabled. That actually meand that unless an explicit transaction is started using BEGIN TRANSACTION, every data modification is started in a separate transaction which is committed after the statement. That allows the database to rollback an entire statement when it fails (for instance a bulk insert, or an insert that modifies other data in a trigger).

    0 讨论(0)
  • 2020-12-09 10:20

    Implicit Transaction is the auto commit. There is no beginning or ending of the transaction.

    Explicit Transaction has the beginning, ending and rollback of transactions with the command Begin Transaction, Commit Transaction and Rollback Transaction.

    In the explicit transaction, if an error occurs in between we can rollback to the beginning of the transaction which cannot be done in implicit transaction.

    0 讨论(0)
  • 2020-12-09 10:33
    • Implicit Transactions: http://msdn.microsoft.com/en-us/library/ms188317.aspx
    • SET IMPLICIT_TRANSACTIONS { ON | OFF} http://msdn.microsoft.com/en-us/library/ms187807.aspx

    Basically, in c# when you set the TransactionScope to Implicit, it calls the SQL Server SET command to put the connection in IMPLICIT_TRANSACTIONS mode. Anything that you do (using one of the commands listed in the 2nd link) starts a transaction that is kept open until a commit is issued. If no commit is issued at the end of a connection, an implicit ROLLBACK is performed.

    This differs from the OFF setting, which also puts every statement into a transaction - the difference is that in the OFF mode (therefore transactions are explicit), each transaction (singular statement) is immediately committed.

    0 讨论(0)
  • 2020-12-09 10:43

    SqlTransaction - which TransactionScope uses under the covers - only sends T-SQL BEGIN TRANSACTION commands for SQL Server 2000 and earlier.

    For SQL Server 2005 and later, the TDS protocol was extended to allow clients to directly manipulate transactions without sending BEGIN TRANSACTION etc. To see these in the Profiler, select the 'TM: Begin Tran starting' events, etc. You will probably need to check the 'Show all events' checkbox to see these events - they are under the Transactions category.

    See Transaction Manager Request in the TDS protocol documentation, TM: Begin Tran Starting Event Class in the Profiler documentation, and SqlInternalTransaction.ExecuteTransactionYukon in the .NET Reference Source.

    0 讨论(0)
提交回复
热议问题