INFO: Understanding ADO Transactions with MS SQL Server This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
This article was previously published under Q198024 On This PageSUMMARY The 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:FETCH ALTER TABLE DELETE INSERT CREATE OPEN GRANT REVOKE DROP TRUNCATE TABLE SELECT UPDATEWhen 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. REFERENCES For additional information, please see the following
article(s) in the Microsoft Knowledge Base: 177138 (http://support.microsoft.com/kb/177138/EN-US/)
INFO: Nested Transactions Not Available in ODBC/OLE DB/ADO
APPLIES TO
| Article Translations
|


Back to the top
