Commitment control

The Db2 for i commitment control support provides a means for processing a group of database changes, such as update, insert, or delete operations or data definition language (DDL) operations, as a single unit of work (also referred to as a transaction).

A commit operation guarantees that the group of operations is completed. A rollback operation guarantees that the group of operations is backed out. A savepoint can be used to break a transaction into smaller units that can be rolled back. A commit operation can be issued through several different interfaces. For example,

  • An SQL COMMIT statement
  • A CL COMMIT command
  • A language commit statement (such as an RPG COMMIT statement)

A rollback operation can be issued through several different interfaces. For example,

  • An SQL ROLLBACK statement
  • A CL ROLLBACK command
  • A language rollback statement (such as an RPG ROLBK statement)

The only SQL statements that cannot be committed or rolled back are:

  • DROP SCHEMA
  • GRANT or REVOKE if an authority holder exists for the specified object

If commitment control was not already started when either an SQL statement is run with an isolation level other than COMMIT(*NONE) or a RELEASE statement is run, then Db2 for i sets up the commitment control environment by running the internal equivalent of the Start Commitment Control (STRCMTCTL) command. Db2 for i specifies the NFYOBJ(*NONE) and CMTSCOPE(*ACTGRP) parameters, along with the LCKLVL parameter. The LCKLVL parameter specified is the lock level on the COMMIT parameter of the Create SQL (CRTSQLxxx), Start SQL Interactive Session (STRSQL), or Run SQL Statements (RUNSQLSTM) command. In REXX, the LCKLVL parameter specified is the lock level on the SET OPTION statement. You can use the STRCMTCTL command to specify a different CMTSCOPE, NFYOBJ, or LCKLVL parameter. If you specify CMTSCOPE(*JOB) to start the job-level commitment definition, Db2 for i uses the job-level commitment definition for programs in that activation group.

Notes:
  • When commitment control is used, the tables that are referred to in the application program by data manipulation language (DML) statements must be journaled.
  • The LCKLVL parameter specified is only the default lock level. After commitment control is started, the SET TRANSACTION SQL statement and the lock level specified on the COMMIT parameter of the CRTSQLxxx, STRSQL, or RUNSQLSTM command will override the default lock level. Also, the LCKLVL parameter only applies to commitment control operations that are requested through the IBM i traditional system interface (non-SQL). The lock level specified on the LCKLVL parameter is not affected by any subsequent changes to the SQL isolation level that are made by using, for example, the SET TRANSACTION statement.

For cursors that use aggregate functions, GROUP BY, or HAVING, and are running under commitment control, a ROLLBACK HOLD has no effect on the cursor's position. In addition, the following occurs under commitment control:

  • If COMMIT(*CHG) and (ALWBLK(*NO) or (ALWBLK(*READ)) is specified for one of these cursors, a message (CPI430B) is sent that says COMMIT(*CHG) requested but not allowed.
  • If COMMIT(*ALL), COMMIT(*RR), or COMMIT(*CS) with the KEEP LOCKS clause is specified for one of the cursors, Db2 for i locks all referenced tables in shared mode (*SHRNUP). The lock prevents concurrent application processes from processing any but read-only operations on the named table. A message (either SQL7902 or CPI430A) is sent that says COMMIT(*ALL), COMMIT(*RR), or COMMIT(*CS) with the KEEP LOCKS clause is specified for one of the cursors requested but not allowed. Message SQL0595 might also be sent.

For cursors where COMMIT(*ALL), COMMIT(*RR), or COMMIT(*CS) with the KEEP LOCKS clause is specified and either catalog files are used or a temporary result table is required, Db2 for i locks all referenced tables in shared mode (*SHRNUP). This prevents concurrent processes from processing anything but read-only operations on the named table or tables. A message (either SQL7902 or CPI430A) is sent that says COMMIT(*ALL) is requested but not allowed. Message SQL0595 might also be sent.

If ALWBLK(*ALLREAD) and COMMIT(*CHG) were specified, when the program was precompiled, all read-only cursors will allow blocking of rows and a ROLLBACK HOLD will not roll the cursor position back.

If COMMIT(*RR) is requested, the tables will be locked until the query is closed. If the cursor is read-only, the table will be locked (*SHRNUP). If the cursor is in update mode, the table will be locked (*EXCLRD). Since other users will be locked out of the table, running with repeatable read will prevent concurrent access of the table.

In a highly contentious environment using COMMIT(*RR), an application might need to retry an operation after getting an SQL0913 to allow the database unlocking mechanism time to work.

If an isolation level other then COMMIT(*NONE) was specified and the application issues a ROLLBACK or the activation group ends abnormally (and the commitment definition is not *JOB), all updates, inserts, deletes, and DDL operations made within the unit of work are backed out. If the application issues a COMMIT or the activation group ends normally, all updates, inserts, deletes, and DDL operations made within the unit of work are committed.

Db2 for i uses locks on rows to keep other jobs from accessing changed data before a unit of work is completed. If COMMIT(*ALL) is specified, read locks on rows fetched are also used to prevent other jobs from changing data that was read before a unit of work is completed. This does not prevent other jobs from reading the unchanged rows. This ensures that, if the same unit of work rereads a row, it gets the same result. Read locks do not prevent other jobs from fetching the same rows.

Start of changeWhen running with COMMIT(*NONE), the database needs to serialize activity as well. To do this, short-lived internal locks are acquired. This means that lock conflicts can be encountered by, or caused by, applications running with COMMIT(*NONE).End of change

Commitment control handles up to 500 million distinct row changes in a unit of work. If COMMIT(*ALL) or COMMIT(*RR) is specified, all rows read are also included in the limit. (If a row is changed or read more than once in a unit of work, it is only counted once toward the limit.) Holding a large number of locks adversely affects system performance and does not allow concurrent users to access rows locked in the unit of work until the end of the unit of work. It is in your best interest to keep the number of rows processed in a unit of work small.

COMMIT HOLD and ROLLBACK HOLD allow you to keep the cursor open and start another unit of work without issuing an OPEN statement again. The HOLD value is not available when you are connected to a remote database that is not on an IBM i platform. However, the WITH HOLD option on DECLARE CURSOR can be used to keep the cursor open after a commit. This type of cursor is supported when you are connected to a remote database that is not on an IBM i platform. Such a cursor is closed on a rollback.

Table 1. Row lock duration
SQL statement COMMIT parameter (see note 5) Duration of row locks Lock type
SELECT INTO
SET variable
VALUES INTO
*NONE No locks (see note 8)    
*CHG No locks (see note 8)
*CS (See note 6) Row locked when read and released READ
*ALL (See note 2 and 7) From read until ROLLBACK or COMMIT READ
FETCH (read-only cursor) *NONE No locks (see note 8)  
*CHG No locks (see note 8)  
*CS (See note 6) From read until the next FETCH READ
*ALL (See note 2 and 7) From read until ROLLBACK or COMMIT READ
FETCH (update or delete capable cursor) (See note 1) *NONE
When row is not updated or deleted
from read until next FETCH
When row is updated
from read until next FETCH
When row is deleted
from read until next DELETE
UPDATE
*CHG
When row is not updated or deleted
from read until next FETCH
When row is updated or deleted
from read until COMMIT or ROLLBACK
UPDATE
*CS

When row is not updated or deleted
from read until next FETCH
When row is updated or deleted
from read until COMMIT or ROLLBACK
UPDATE
*ALL From read until ROLLBACK or COMMIT UPDATE
INSERT (target table)
MERGE, INSERT sub-statement
*NONE No locks (see note 8)  
*CHG From insert until ROLLBACK or COMMIT UPDATE
*CS From insert until ROLLBACK or COMMIT UPDATE
*ALL From insert until ROLLBACK or COMMIT UPDATE3
INSERT (tables in subselect) *NONE No locks (see note 8)  
*CHG No locks (see note 8)  
*CS Each row locked while being read READ
*ALL From read until ROLLBACK or COMMIT READ
UPDATE (non-cursor) *NONE Each row locked while being updated UPDATE
*CHG From read until ROLLBACK or COMMIT UPDATE
*CS From read until ROLLBACK or COMMIT UPDATE
*ALL From read until ROLLBACK or COMMIT UPDATE
DELETE (non-cursor) *NONE Each row locked while being deleted UPDATE
*CHG From read until ROLLBACK or COMMIT UPDATE
*CS From read until ROLLBACK or COMMIT UPDATE
*ALL From read until ROLLBACK or COMMIT UPDATE
UPDATE (with cursor) *NONE From read until next FETCH UPDATE
*CHG From read until ROLLBACK or COMMIT UPDATE
*CS From read until ROLLBACK or COMMIT UPDATE
*ALL From read until ROLLBACK or COMMIT UPDATE
MERGE UPDATE sub-statement
*NONE From read until MERGE statement completion UPDATE
*CHG From read until ROLLBACK or COMMIT UPDATE
*CS From read until ROLLBACK or COMMIT UPDATE
*ALL From read until ROLLBACK or COMMIT UPDATE
DELETE (with cursor) *NONE Lock released when row deleted UPDATE
*CHG From read until ROLLBACK or COMMIT UPDATE
*CS From read until ROLLBACK or COMMIT UPDATE
*ALL From read until ROLLBACK or COMMIT UPDATE
MERGE, DELETE sub-statement
*NONE From read until MERGE statement completion UPDATE
*CHG From read until ROLLBACK or COMMIT UPDATE
*CS From read until ROLLBACK or COMMIT UPDATE
*ALL From read until ROLLBACK or COMMIT UPDATE
Subqueries (update or delete capable cursor or UPDATE or DELETE non-cursor) *NONE From read until next FETCH READ
*CHG From read until next FETCH READ
*CS From read until next FETCH READ
*ALL (see note 2) From read until ROLLBACK or COMMIT READ
Subqueries (read-only cursor or SELECT INTO) *NONE No locks (see note 8)  
*CHG No locks (see note 8)  
*CS Each row locked while being read READ
*ALL From read until ROLLBACK or COMMIT READ
Notes:
  1. A cursor is open with UPDATE or DELETE capabilities if the result table is not read-only and if one of the following is true:
    • The cursor is defined with a FOR UPDATE clause.
    • The cursor is defined without a FOR UPDATE, FOR READ ONLY, or ORDER BY clause and the program contains at least one of the following:
      • Cursor UPDATE referring to the same cursor-name
      • Cursor DELETE referring to the same cursor-name
      • An EXECUTE or EXECUTE IMMEDIATE statement and ALWBLK(*READ) or ALWBLK(*NONE) was specified on the CRTSQLxxx command.
  2. A table or view can be locked exclusively in order to satisfy COMMIT(*ALL). If a subselect is processed that includes a UNION, or if the processing of the query requires the use of a temporary result, an exclusive lock is acquired to protect you from seeing uncommitted changes.
  3. An UPDATE lock on rows of the target table and a READ lock on the rows of the subselect table.
  4. A table or view can be locked exclusively in order to satisfy repeatable read. Row locking is still done under repeatable read. The locks acquired and their duration are identical to *ALL.
  5. Repeatable read (*RR) row locks will be the same as the locks indicated for *ALL.
  6. If the KEEP LOCKS clause is specified with *CS, any read locks are held until the cursor is closed or until a COMMIT or ROLLBACK is done. If no cursors are associated with the isolation clause, then locks are held until the completion of the SQL statement.
  7. If the USE AND KEEP EXCLUSIVE LOCKS clause is specified with the *RS or *RR isolation level, an UPDATE lock on the row will be obtained instead of a READ lock.
  8. Start of changeWhile the query is executing, short-lived internal locks could be held.End of change