# Switching Transaction Modes

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

+ Pessimistic mode is the default mode
+ Start a transaction in pessimistic mode explicitly
  + **`BEGIN PESSIMISTIC`**
+ Start a transaction in optimistic mode explicitly
  + **`BEGIN OPTIMISTIC`** 
+ Start a transaction in pessimistic mode implicitly
  + Execute **`SET tidb_txn_mode='pessimistic'`** and then **`BEGIN`**
+ Start a transaction in optimistic mode implicitly
  + Execute **`SET tidb_txn_mode='optimistic'`** and then **`BEGIN`** 

------------------------------------------------------------
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.
