|
|
|
|
Transaction ManagementStarting a TransactionIn SQL, there is no explicit statement that starts a transaction. A transaction will be started implicitly (if one isn't already started) whenever you execute a SQL statement that requires a transaction context. If you don't tell the system differently, the transaction will have the following default characteristics:
If you want to change these characteristics, you must use the SET TRANSACTION statement to explicitly set them: SET TRANSACTION READ ONLY, ISOLATION LEVEL READ UNCOMMITTED The SET TRANSACTION statement must be the first statement executed in a transaction. Committing a TransactionOnce all the operations in a transaction have successfully completed, you can commit those changes by using the COMMIT statement: COMMIT; Rolling Back a TransactionIf you decide that a transaction's actions should not be (or cannot be) completed, you can roll those actions back by using the ROLLBACK statement: ROLLBACK; Mixing DML and DDL in a TransactionSome database systems allow you to mix DML and DDL in a single transaction. For example:
Other database systems to not allow you do mix the two. If you attempt to mix them, you may encounter errors, or you may find that a single DDL statement execution will cause the transaction to commit implicitly. If you are writing code that should run against several database systems, you would be well advised to avoid such mixing of DML and DDL. AutoCommit ModeSome systems (such as ODBC and JDBC) have a mode called autocommit mode, which causes an implicit and automatic commit to occur after every SQL statement execution. For anything other than a trivial transaction, this is not an appropriate choice. |
| The page was last updated February 19, 2008 |