胡志明市到大叻:Transactions

来源:百度文库 编辑:偶看新闻 时间:2024/05/08 00:45:56

Transactions

A transaction is a unit of work that is done as asingle operation. There are two base characteristics of transaction:

  • Transaction mode
  • TransactionIsolation Level

Transaction mode

Transactions can be completed by either being committedor being rolled back. When a transaction is committed, thechanges made in that transaction are made permanent. When a transactionis rolled back, the affected rows are returned to the state they werein before the transaction was started.

Transactions can be in one of two modes: auto-commitmode or manual-commit mode.

In auto-commit mode, every database operation is atransaction that is committed when performed. In databases withouttransaction support, auto-commit mode is the only supported mode. Insuch databases, statements are committed when they are executed andthere is no way to roll them back; they are therefore always inauto-commit mode.

In manual-commit mode, applications must explicitlycomplete transactions by calling SAConnection::Commit tocommit them or SAConnection::Rollbackto roll them back. This is the normal transaction mode for mostrelational databases.

Note: The default transaction settings areDBMS-defined.

Using SQLAPI++ to set auto-commit mode compulsory you shouldcall SAConnection::setAutoCommitmethod with parameter value SA_AutoCommitOn. To setmanual-commit mode you should call SAConnection::setAutoCommitmethod with parameter value SA_AutoCommitOff. If youdidn't set transaction mode explicitly then the Library uses thedefault DBMS transaction settings. See DBMS documentation to getinformation concerning the default transaction mode.

If you disconnected from server without commit, then theresult of transaction depends on underlying DBMS. It can either beingcommitted or being rolled back. See DBMS documentation.

Calling SAConnection::Commitor SAConnection::Rollbackin auto-commit is safe (it has no effect). If you are working inmanual-commit mode and call SAConnection::Commitmethod then current transaction is committed and new one is started. Ifyou are working in manual-commit mode and call SAConnection::Rollbackmethod then current transaction is rolled back and new one is started.

Transaction IsolationLevel

Transaction isolation refers to the degree ofinteraction between multiple concurrent transactions. SQL-92 definesfour isolation levels, all of which are supported by SQLAPI++:

  • Read uncommitted (the lowest level where transactions areisolated just enough to ensure that physically corrupt data is notread)
  • Read committed
  • Repeatable read
  • Serializable (the highest level, where transactions arecompletely isolated from one another)

SQLAPI++ maps different isolation levels on underlying DBMS.You can set transaction isolation level explicitly by calling SAConnection::setIsolationLevel.

If you didn't set transaction isolation level explicitly thenthe Library uses the default DBMS transaction settings. See DBMSdocumentation to get information concerning the default transactionisolation level.

Note that if you change isolation level it causes implicitcommit for this connection.

Problems and Questions

If you haven't found the answer to your questions or have someproblems on using the Library, please, send e-mail to howto@sqlapi.com.