The information in this article applies to:
SUMMARYThe ActiveX Data Objects (ADO) Connection method BeginTrans states that it begins a new Transaction, but it executes "set implicit_transactions on." In many cases, "set implicit_transactions on" is equivalent to T-SQL BEGIN TRANS. However, there are subtle differences. This article illustrates one of those differences. MORE INFORMATIONHow Does ADO Behave with Respect to TransactionsBy default ADO operates in AutoCommit mode, unless you start a implicit transaction by executing Connection.BeginTrans.Implicit_transactions begin a transaction on the server for each statement, and commits do not occur until they are manually issued. So,
is internally turned into
The above transaction will not be rolled back or committed unless the user
issues the correct statement.
Without implicit transaction, which by default is the behavior of ADO (Auto Commit mode), the following is (conceptually) occurring:
The following code sequence, written in Visual Basic, shows a difference
between the raw SQL "BEGIN TRANSACTION" and the "set implicit_transactions
on" issued when the ADO connection method BeginTrans is invoked:
Reason for the Difference in ResultsWith ADO Connection.BeginTrans will not increment @@TranCount until any of the following statements are encountered:
When this option (set implicit_transactions on) is turned on and if there
are no outstanding transactions, every ANSI SQL statement will
automatically start a transaction. If there is an open transaction, no new
transaction will be started. This transaction has to be explicitly
committed by the user by using the command COMMIT TRANSACTION for the
changes to take affect and the locks to be released.
Whereas executing Cn.Execute "BEGIN TRANSACTION" starts an explicit transaction and @@TranCount is immediately incremented by 1. In the above Visual Basic code for the procedure Command1_Click() the @@TranCount remains at 0 and the Rollback has no effect because before executing the first insert, you revert back to Auto Commit mode by issuing Cn.Execute "set implicit_transactions off." Whereas in the Visual Basic code for procedure Command2_Click(), the @@TranCount is immediately incremented to 1 when you issue Cn.Execute "BEGIN TRANSACTION; hence, the subsequent RollBack works. The SQL Server 6.5 Trace utility is very useful for observing the Transaction commands this sample issues. REFERENCESFor additional information, please see the following article(s) in the Microsoft Knowledge Base: Q177138 INFO: Nested Transactions Not Available in ODBC/OLE DB/ADO © Microsoft Corporation 1998, All Rights Reserved. Additional query words: kbSQLServer700
Keywords : kbcode kbADO200 kbDatabase kbGrpMDAC kbDSupport kbADO210sp2 kbGrpODBC |
Last Reviewed: November 18, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |