|
|
Transaction Control Statements
The following sections explain how to start, end and optimize transactions. You can also read about consistency and exception diagnostics in transactions. Further, transaction options, cursors and error handling are discussed.
Starting Transactions
Transaction start may be set to
EXPLICITorIMPLICIT.The default transaction start setting is
IMPLICIT, which means a transaction will be started automatically whenever one is needed.To set the transaction start mode, use the statements:
SET TRANSACTION START EXPLICIT; SET TRANSACTION START IMPLICIT;Different database connections can use different transaction start options.
The
STARTstatement can always be used to explicitly start a transaction. This is useful if a number of related updates are to be performed and it is desirable that all the updates succeed or fail together to maintain consistency.You cannot start a transaction while a transaction is already active.
Explicit Transaction Start
With this setting, transactions are never automatically started. All transactions must be explicitly started by executing the
STARTstatement.Any update operation (
INSERT,UPDATEorDELETE) involving a table in a databank with theTRANSorLOGoption must occur within a transaction. An error will be raised if such an update is attempted without first starting a transaction.All the statements issued after the
STARTstatement and before the transaction is concluded are grouped together within that single transaction.A transaction is concluded by executing a
COMMITorROLLBACKstatement.Implicit Transaction Start
With this setting, a transaction is started automatically (if one is not already active) by a reference to an object stored in a databank with the
TRANSorLOGoption (i.e. if none of the objects referenced are stored in a databank with theTRANSorLOGoption, no transaction is required and therefore one is not started).The
STARTstatement may be used to explicitly start a transaction if required, typically to allow several updates to be grouped together within a single transaction for consistency, as already described.An automatically started transaction is concluded by executing a
COMMITorROLLBACKstatement.All the statements issued after the initiating update and before the concluding
COMMITorROLLBACKstatement are grouped together within that single transaction.Ending Transactions
Transactions must be ended with the
COMMITorROLLBACKstatement.
- COMMIT
This statement requests that the operations in the write-set are executed on the database, making the changes permanent and visible to other users. The
SQLSTATEvalue returned when aCOMMITstatement is executed indicates either that the transaction commitment was successful (SQLSTATE = '00000') or that a transaction conflict occurred (SQLSTATE <> '00000').- ROLLBACK
This statement abandons the transaction. The read-set and write-set are dropped and no changes are made to the database.
ROLLBACKis always successful.
- Note: A transaction in Mimer SQL is never physically rolled-back in the sense of undoing changes made to the database, since changes are not actually effected until a successful
COMMITis performed. However, theROLLBACKstatement may free internal resources.Transactions that are not successfully committed due to a transaction conflict do not have to be explicitly rolled back. The
ROLLBACKstatement is most commonly used in exception routines for handling error situations that are detected by the application during transaction build-up.If a connection or program is terminated without requesting a
COMMITorROLLBACKfor the current transaction, the system will abort the transaction. None of the changes requested during the transaction build-up will be made to the database.Transaction handling in BSQL differs slightly from that described here - see the Mimer SQL User's Manual, Handling Transactions, for details.
Optimizing Transactions
The following
SET TRANSACTIONoptions are used to optimize transaction performance:
- READ ONLY
This setting should always be used for transactions that do not require update access to the database. Significant performance gains can be achieved, especially for queries retrieving large numbers of rows, when this setting is used in queries when there is no need for update access to the database.
- READ WRITE
This setting should only be used for transactions that require update access to the database. This is the default setting for a transaction.
The default option is
READ WRITE, or the option defined to be the default for the current session by using theSET SESSIONstatement, see Setting Default Transaction Options.The
SET TRANSACTION READcommand only affects the single next transaction started after it is used.Consistency Within Transactions
The
SET TRANSACTION ... ISOLATION LEVELoptions can be used to control the degree to which the changes occurring within one transaction are affected by the changes occurring within other concurrently executing transactions.The default option is
REPEATABLE READ, or the option defined to be the default for the current session by using theSET SESSIONstatement, see Setting Default Transaction Options.The
SET TRANSACTION ... ISOLATION LEVELcommand only affects the single next transaction started after it is used.Options
The following options are available:
- SERIALIZABLE
This setting guarantees that the end result of the operations performed by two or more concurrent transactions will be the same as if the transactions had been executed in a serial fashion, where one executes to completion before the other starts.
- REPEATABLE READ
This setting offers the same consistency guarantee as serializable, except that the concurrency effect known as phantoms may be encountered (see below for a reference to the definition of this concurrency effect).
- READ COMMITTED
This setting offers the same consistency guarantee as repeatable read, except that the concurrency effect known as non-repeatable read may also be encountered (see below for a reference to the definition of this concurrency effect).
- READ UNCOMMITTED
This setting offers the same consistency guarantee as read committed, except that the concurrency effect known as dirty read may also be encountered (see below for a reference to the definition of this concurrency effect).
For a definition of the concurrency effects mentioned above (phantoms, non-repeatable read and dirty read) refer to the Mimer SQL Reference Manual, SET TRANSACTION.
All of the isolation level settings guarantee that each transaction will be executed completely or not at all and that no updates will be lost.
Exception Diagnostics Within Transactions
The
SET TRANSACTION DIAGNOSTICS SIZEoption allows the size of the diagnostics area to be defined. An unsigned integer value specifies how many exceptions can be stacked in the diagnostics area, and examined byGET DIAGNOSTICS, see the Mimer SQL Reference Manual, GET DIAGNOSTICS, in situations where repeatedRESIGNALoperations have effectively been performed.The
SET TRANSACTION DIAGNOSTICS SIZEsetting only affects the single next transaction to be started.The default
SET TRANSACTION DIAGNOSTICS SIZEsetting (50 or whatever has been defined to be the default by usingSET SESSION) applies unless an alternative is explicitly set before each transaction.Setting Default Transaction Options
The
SET SESSIONstatement can be used to define the default settings for the transaction options set bySET TRANSACTION READ,SET TRANSACTION ISOLATION LEVELandSET TRANSACTION DIAGNOSTICS SIZE.As these
SET TRANSACTIONcommands only affect the single next transaction started after they are used, it is often convenient to define the desired default options for each of them.A detailed description of theSET SESSIONstatement can be found in the Mimer SQL Reference Manual.Statements in Transactions
The tables that follow summarize whether statements may or may not be used inside transactions.
Access Control Statements
Connection Statements
Statements
Allowed
Comments
CONNECTSET CONNECTION YesDISCONNECT Yes AROLLBACKis performed on any active transaction.ENTERLEAVE(program ident) NoData Definition Statements
Statements
Allowed
Comments
ALTERCOMMENTCREATEDROP Yes Must be the only statement in a transaction.Data Manipulation Statements
Declarative Statements
Statements
Allowed
Comments
DECLARE CONDITIONDECLARE CURSORDECLARE HANDLERDECLARE VARIABLE Not applicable Declarative statementDiagnostic Statements
Dynamic SQL Statements
Statements
Allowed
Comments
PREPAREDESCRIBEEXECUTEEXECUTE IMMEDIATEEXECUTE STATEMENTALLOCATE CURSORALLOCATE DESCRIPTORDEALLOCATE DESCRIPTORDEALLOCATE PREPAREGET DESCRIPTORSET DESCRIPTOR Yes See Dynamic SQL.ESQL Control Statements
Procedure Control Statements
Statements
Allowed
Comments
CALLCASECOMPOUND STATEMENTFORIFITERATELEAVELOOPREPEATRETURNSETWHILE YesSystem Administration Statements
Transaction Control Statements
Statements
Allowed
Comments
SET SESSIONSET TRANSACTIONSTART No These statements control transaction behaviorCOMMITROLLBACK YesCursors in Transactions
A cursor open by the current connection may be closed implicitly by one of the transaction terminating statements
COMMITandROLLBACK.ROLLBACKcloses all open cursors for the current connection.COMMITcloses all open cursors for the current connection, except cursors declaredWITH HOLD. Holdable cursors remain open afterCOMMIT.When a stacked cursor is closed, all instances of the cursor are closed.
Cursors are also closed implicitly by
LEAVEandDISCONNECT. InSET TRANSACTION START EXPLICITmode, cursors may be opened and used outside transactions. Such cursors remain accessible when anENTERstatement is issued, and remain open when aLEAVEstatement is issued.This is illustrated in the following statement sequence:
... EXEC SQL SET TRANSACTION START EXPLICIT; EXEC SQL DECLARE c_1 CURSOR FOR SELECT col1 FROM tab1; EXEC SQL DECLARE c_2 CURSOR FOR SELECT col2 FROM tab2 WHERE checkcol = :VAR1; EXEC SQL OPEN c_1; loop EXEC SQL FETCH c_1 INTO :VAR1; -- Fetch value from tab1 EXEC SQL ENTER ... ; -- Change current ident EXEC SQL OPEN c_2; EXEC SQL FETCH c_2 INTO ...; -- Fetch row for c_2 EXEC SQL CLOSE c_2; EXEC SQL LEAVE; end loop; ...In the above example, the value fetched for the cursor
C1is used to determine the set of rows addressed by cursorC2. CursorC1remains open and positioned during theENTER ... LEAVEsequence.Each time the loop is executed, a new value is fetched by
C1and a new set of rows is addressed byC2. The same behavior applies whenLEAVE RETAINis used to leave aPROGRAMident but keep the environment for the ident.A cursor opened and used outside a transaction may however not be used within a transaction. If the same cursor is required outside and inside a transaction, separate instances must be opened. Remember that separate instances of a cursor address separate result sets:
... EXEC SQL SET TRANSACTION START EXPLICIT; EXEC SQL DECLARE c_1 REOPENABLE CURSOR FOR SELECT col1 FROM tab1; EXEC SQL OPEN c_1; EXEC SQL FETCH c_1 INTO ...; -- First row (outside transaction) ... EXEC SQL START; EXEC SQL OPEN c_1; -- New instance of cursor EXEC SQL FETCH c_1 INTO ...; -- First row again ...Error Handling in Transactions
In general, errors and exception conditions are reported in
SQLSTATEafter each executable SQL statement.The value of
SQLSTATEindicates the outcome of the preceding statement, see SQLSTATE Return Codes for a list of SQLSTATE values.
GET DIAGNOSTICScan be used to get detailed status information after an SQL statement.The value of
SQLSTATEafter aCOMMITstatement indicates the success or failure of the request to commit the transaction, not the outcome of any data manipulations performed within the transaction.About WHENEVER
Use of the general error handling statement
WHENEVER, see the Mimer SQL Reference Manual, WHENEVER, for a description) in transactions requires some care:
- Program control can be transferred to an exception routine in the event of an error. Make sure that the exception routine is designed to take care of uncompleted transactions.
Most commonly, the first SQL statement in the exception routine should beGET DIAGNOSTICS. The exception routine should normally also execute aROLLBACKstatement. Remember that if the exception routine is used from a statement outside a transaction, any open cursors belonging to the current ident will be closed by theROLLBACKstatement.GET DIAGNOSTICScan be used to determine whether or not a transaction is active.- For transaction conflict, the
SQLSTATEvalue returned from theCOMMITstatement falls into theSQLERRORclass. If the transaction is to be retried in the event of conflict, make sure that noWHENEVER SQLERROR GOTO exceptionstatement is operative.If
WHENEVERerror handling is used in an application program, a suitable program structure forCOMMITstatements is:EXEC SQL WHENEVER SQLERROR GOTO exception; ... EXEC SQL WHENEVER SQLERROR GOTO retry; EXEC SQL COMMIT; EXEC SQL WHENEVER SQLERROR GOTO exception; ...
|
Mimer Information Technology AB Voice: +46 18 780 92 00 Fax: +46 18 780 92 40 info@mimer.se |
|
|