SET TRANSACTION

The SET TRANSACTION statement sets the isolation level, read only attribute, or diagnostics area size for the current unit of work.

Invocation

This statement can be embedded within an application program or issued interactively. It is an executable statement that can be dynamically prepared.

Authorization

Start of change If a global variable is referenced in the statement, the privileges held by the authorization ID of the statement must include at least one of the following:End of change

Start of change
  • For the global variable identified in the statement,
    • The READ privilege on the global variable, and
    • The system authority *EXECUTE on the library containing the global variable
  • Administrative authority
End of change

Syntax

Read syntax diagramSkip visual syntax diagram
>>-SET TRANSACTION---------------------------------------------->

   .-,-----------------------------------------.       
   V                    .-SERIALIZABLE-----.   | (1)   
>----+-ISOLATION LEVEL--+-NO COMMIT--------+-+-+---------------><
     |                  +-READ UNCOMMITTED-+ |         
     |                  +-READ COMMITTED---+ |         
     |                  '-REPEATABLE READ--' |         
     | .-READ ONLY--.                        |         
     +-+-READ WRITE-+------------------------+         
     '-DIAGNOSTICS SIZE--+-integer--+--------'         
                         '-variable-'                  

Notes:
  1. Only one ISOLATION LEVEL clause, one READ WRITE or READ ONLY clause, and one DIAGNOSTICS SIZE clause may be specified.

Description

ISOLATION LEVEL
Specifies the isolation level of the transaction. If the ISOLATION LEVEL clause is not specified, ISOLATION LEVEL SERIALIZABLE is implicit
NO COMMIT
Specifies isolation level NC (COMMIT(*NONE)).
READ UNCOMMITTED
Specifies isolation level UR (COMMIT(*CHG)).
READ COMMITTED
Specifies isolation level CS (COMMIT(*CS)).
REPEATABLE READ
Specifies isolation level RS (COMMIT(*ALL)).
SERIALIZABLE
Specifies isolation level RR (COMMIT(*RR)).
READ WRITE or READ ONLY
Specifies whether the transaction allows data change operations.
READ WRITE
Specifies that all SQL operations are allowed. This is the default unless ISOLATION LEVEL READ UNCOMMITTED is specified.
READ ONLY
Specifies that only SQL operations that do not change SQL data are allowed. If ISOLATION LEVEL READ UNCOMMITTED is specified, this is the default.
DIAGNOSTICS SIZE
Specifies the maximum number of GET DIAGNOSTICS condition areas for the current transaction. The GET DIAGNOSTICS statement-information-item MORE will be set to 'Y' for the current statement if the statement exceeds the maximum number of condition areas for the current transaction. Start of changeThe maximum size of the diagnostics area is 90K.End of change The specified maximum number of condition areas must be between 1 and 32767.
integer
An integer constant that specifies the maximum number of condition areas for the current transaction.
variable
Identifies a variable which contains the maximum number of condition areas for the current transaction. The variable must be a numeric variable with a zero scale and must not be followed by an indicator variable.

Notes

Scope of SET TRANSACTION: The SET TRANSACTION statement sets the isolation level for SQL statements for the current activation group of the process. If that activation group has commitment control scoped to the job, then the SET TRANSACTION statement sets the isolation level of all other activation groups with job commit scoping as well.

Start of changeIf an isolation clause is specified in an SQL statement, that isolation level overrides the transaction isolation level and is used for dynamic SQL statements.End of change

The scope of the SET TRANSACTION statement is based on the context in which it is run. If the SET TRANSACTION statement is run in a trigger, the isolation level specified applies to all subsequent SQL statements until another SET TRANSACTION statement occurs or until the trigger completes, whichever happens first. If the SET TRANSACTION statement is run outside a trigger, the isolation level specified applies to all subsequent SQL statements (except those statements within a trigger that are executed after a SET TRANSACTION statement in the trigger) until a COMMIT or ROLLBACK operation occurs.

For more information about isolation levels, see Isolation level.

SET TRANSACTION restrictions: The SET TRANSACTION statement can only be executed when it is the first SQL statement in a unit of work, unless:

  • all previous statements executed in the unit of work are SET TRANSACTION statements or statements that are executed under isolation level NC, or
  • it is executed in a trigger.

In a trigger, SET TRANSACTION with READ ONLY is allowed only on a COMMIT boundary. The SET TRANSACTION statement can be executed in a trigger at any time, but it is recommended that it be executed as the first statement in the trigger. The SET TRANSACTION statement is useful within triggers to set the isolation level for SQL statements in the trigger to the same level as the application which caused the trigger to be activated.

A SET TRANSACTION statement is not allowed if the current connection is to a remote application server unless it is in a trigger at the current server. Once a SET TRANSACTION statement is executed, CONNECT and SET CONNECTION statements are not allowed until the unit of work is committed or rolled back.

SET TRANSACTION is not allowed as the first statement in a secondary thread.

The SET TRANSACTION statement has no effect on WITH HOLD cursors that are still open when the SET TRANSACTION statement is executed.

Syntax alternatives: The following keywords are synonyms supported for compatibility to prior releases. These keywords are non-standard and should not be used:

  • The keywords NC or NONE can be used as synonyms for NO COMMIT.
  • The keywords UR and CHG can be used as synonyms for READ UNCOMMITTED.
  • The keyword CS can be used as a synonym for READ COMMITTED.
  • The keywords RS or ALL can be used as synonyms for REPEATABLE READ.
  • The keyword RR can be used as a synonym for SERIALIZABLE.

Examples

Example 1: The following SET TRANSACTION statement sets the isolation level to NONE (equivalent to specifying *NONE on the SQL precompiler command).

   EXEC SQL SET TRANSACTION ISOLATION LEVEL NO COMMIT;

Example 2: The following SET TRANSACTION statement sets the isolation level to SERIALIZABLE.

   SET TRANSACTION ISOLATION LEVEL SERIALIZABLE