# Switching Transaction Modes

__*Pessimistic locking*__
__*Optimistic locking*__
__*BEGIN OPTIMISTIC*__
__*BEGIN PESSIMISTIC*__

+ Start a transaction in pessimistic mode
  + `BEGIN PESSIMISTIC;` 
    + or
  + `SET tidb_txn_mode='pessimistic';` 
  + `BEGIN;`
+ Start a transaction in optimistic mode
  + `BEGIN OPTIMISTIC;` 
    + or
  + `SET tidb_txn_mode='optimistic';` 
  + `BEGIN;` 
+ Pessimistic mode is the default mode
------------------------------------------------------------
You can set the transaction mode by configuring the tidb_txn_mode system variable.

The following command sets all explicit transactions (that is, transactions that are not auto-committed) performed by newly created sessions in the cluster to pessimistic transaction mode: `SET GLOBAL tidb_txn_mode = 'pessimistic';`  

You can also explicitly enable pessimistic transaction mode by executing the following SQL statement: BEGIN PESSIMISTIC;. 

The BEGIN PESSIMISTIC and BEGIN PESSIMISTIC statements take precedence over the tidb_txn_mode system variable. transaction that begin with these two statements ignore system variables.
