Monday, August 31, 2009

MCTS 70-433 implicit transaction and default behaviour

Coming from an oracle background, I'm used to having explicitly commit my transactions as the default behaviour in the database, in SQL Server this is not always the default:

After implicit transaction mode has been set on for a connection, SQL Server automatically starts a transaction when it first executes any of these statements:

ALTER TABLE

INSERT

CREATE

OPEN

DELETE

REVOKE

DROP

SELECT

FETCH

TRUNCATE TABLE

GRANT

UPDATE


The transaction remains in effect until you issue a COMMIT or ROLLBACK statement...

Implicit transaction mode is set either using the Transact-SQL SET statement, or through database API functions and methods.

http://msdn.microsoft.com/en-us/library/aa213064(SQL.80).aspx




The API mechanisms used to set implicit transactions are ODBC and OLE DB.

ODBC

  • Call the SQLSetConnectAttr function with Attribute set to SQL_ATTR_AUTOCOMMIT and ValuePtr set to SQL_AUTOCOMMIT_OFF to start implicit transaction mode.
  • The connection remains in implicit transaction mode until you call SQLSetConnectAttr with Attribute set to SQL_ATTR_AUTOCOMMIT and ValuePtr set to SQL_AUTOCOMMIT_ON.
  • Call the SQLEndTran function with CompletionType set to either SQL_COMMIT or SQL_ROLLBACK to commit or roll back each transaction.
  • When SQL_AUTOCOMMIT_OFF is set by an ODBC application, the Microsoft® SQL Server™ ODBC driver issues a SET IMPLICIT_TRANSACTION ON statement
    http://msdn.microsoft.com/en-us/library/aa213067(SQL.80).aspx

No comments: