How to set Isolation Level for each transaction in Golang

psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
   "password=%s dbname=%s sslmode=disable",
   host, port, user, password, dbname)

db, err := sqlx.Open("postgres", psqlInfo)

// create a TX
tx, err := db.BeginTxx(context, &sql.TxOptions{Isolation: sql.LevelDefault})
rows, err := tx.Query(`select amount from person where id=$1`, 1)
....

Belows is some types of level in Golang support


// IsolationLevel is the transaction isolation level used in TxOptions.type IsolationLevel int
// Various isolation levels that drivers may support in BeginTx.
// If a driver does not support a given isolation level an error may be returned.
//// See https://en.wikipedia.org/wiki/Isolation_(database_systems)#Isolation_levels.
const (
LevelDefault IsolationLevel = iota   
LevelReadUncommitted   
LevelReadCommitted   
LevelWriteCommitted   
LevelRepeatableRead   
LevelSnapshot   
LevelSerializable   
LevelLinearizable
)

Comments

Popular posts from this blog

Fixing the DeepSpeed Import Error While Fine-Tuning the Qwen Model

Amazon Linux 2023 - User data configuration for launch templates to connect to the EKS cluster

How to create ISM policy and rotate logs in opensearch