Rechercher dans le manuel MySQL

13.3.7 SET TRANSACTION Syntax

  1.     transaction_characteristic [, transaction_characteristic] ...
  2.  
  3. transaction_characteristic: {
  4.     ISOLATION LEVEL level
  5.   | access_mode
  6. }
  7.  
  8. level: {
  9. }
  10.  
  11. access_mode: {
  12.      READ WRITE
  13.    | READ ONLY
  14. }

This statement specifies transaction characteristics. It takes a list of one or more characteristic values separated by commas. Each characteristic value sets the transaction isolation level or access mode. The isolation level is used for operations on InnoDB tables. The access mode specifies whether transactions operate in read/write or read-only mode.

In addition, SET TRANSACTION can include an optional GLOBAL or SESSION keyword to indicate the scope of the statement.

Transaction Isolation Levels

To set the transaction isolation level, use an ISOLATION LEVEL level clause. It is not permitted to specify multiple ISOLATION LEVEL clauses in the same SET TRANSACTION statement.

The default isolation level is REPEATABLE READ. Other permitted values are READ COMMITTED, READ UNCOMMITTED, and SERIALIZABLE. For information about these isolation levels, see Section 15.7.2.1, “Transaction Isolation Levels”.

Contents Haut

Transaction Access Mode

To set the transaction access mode, use a READ WRITE or READ ONLY clause. It is not permitted to specify multiple access-mode clauses in the same SET TRANSACTION statement.

By default, a transaction takes place in read/write mode, with both reads and writes permitted to tables used in the transaction. This mode may be specified explicitly using SET TRANSACTION with an access mode of READ WRITE.

If the transaction access mode is set to READ ONLY, changes to tables are prohibited. This may enable storage engines to make performance improvements that are possible when writes are not permitted.

In read-only mode, it remains possible to change tables created with the TEMPORARY keyword using DML statements. Changes made with DDL statements are not permitted, just as with permanent tables.

The READ WRITE and READ ONLY access modes also may be specified for an individual transaction using the START TRANSACTION statement.

Contents Haut

Transaction Characteristic Scope

You can set transaction characteristics globally, for the current session, or for the next transaction only:

  • With the GLOBAL keyword:

    • The statement applies globally for all subsequent sessions.

    • Existing sessions are unaffected.

  • With the SESSION keyword:

    • The statement applies to all subsequent transactions performed within the current session.

    • The statement is permitted within transactions, but does not affect the current ongoing transaction.

    • If executed between transactions, the statement overrides any preceding statement that sets the next-transaction value of the named characteristics.

  • Without any SESSION or GLOBAL keyword:

    • The statement applies only to the next single transaction performed within the session.

    • Subsequent transactions revert to using the session value of the named characteristics.

    • The statement is not permitted within transactions:

      1. Query OK, 0 rows affected (0.02 sec)
      2.  
      3. ERROR 1568 (25001): Transaction characteristics can't be changed
      4. while a transaction is in progress

A change to global transaction characteristics requires the CONNECTION_ADMIN or SUPER privilege. Any session is free to change its session characteristics (even in the middle of a transaction), or the characteristics for its next transaction (prior to the start of that transaction).

To set the global isolation level at server startup, use the --transaction-isolation=level option on the command line or in an option file. Values of level for this option use dashes rather than spaces, so the permissible values are READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, or SERIALIZABLE.

Similarly, to set the global transaction access mode at server startup, use the --transaction-read-only option. The default is OFF (read/write mode) but the value can be set to ON for a mode of read only.

For example, to set the isolation level to REPEATABLE READ and the access mode to READ WRITE, use these lines in the [mysqld] section of an option file:

[mysqld]
transaction-isolation = REPEATABLE-READ
transaction-read-only = OFF

At runtime, characteristics at the global, session, and next-transaction scope levels can be set indirectly using the SET TRANSACTION statement, as described previously. They can also be set directly using the SET statement to assign values to the transaction_isolation and transaction_read_only system variables:

  • SET TRANSACTION permits optional GLOBAL and SESSION keywords for setting transaction characteristics at different scope levels.

  • The SET statement for assigning values to the transaction_isolation and transaction_read_only system variables has syntaxes for setting these variables at different scope levels.

The following tables show the characteristic scope level set by each SET TRANSACTION and variable-assignment syntax.

Table 13.9 SET TRANSACTION Syntax for Transaction Characteristics

Syntax Affected Characteristic Scope
SET GLOBAL TRANSACTION transaction_characteristic Global
SET SESSION TRANSACTION transaction_characteristic Session
SET TRANSACTION transaction_characteristic Next transaction only

Table 13.10 SET Syntax for Transaction Characteristics

Syntax Affected Characteristic Scope
SET GLOBAL var_name = value Global
SET @@GLOBAL.var_name = value Global
SET PERSIST var_name = value Global
SET @@PERSIST.var_name = value Global
SET PERSIST_ONLY var_name = value No runtime effect
SET @@PERSIST_ONLY.var_name = value No runtime effect
SET SESSION var_name = value Session
SET @@SESSION.var_name = value Session
SET var_name = value Session
SET @@var_name = value Next transaction only

It is possible to check the global and session values of transaction characteristics at runtime:

  1. SELECT @@GLOBAL.transaction_isolation, @@GLOBAL.transaction_read_only;
  2. SELECT @@SESSION.transaction_isolation, @@SESSION.transaction_read_only;

Find a PHP function

Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-set-transaction.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut