Posts

Showing posts with the label Transaction Isolation

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

Transaction Isolation

Image
Ref: https://www.postgresql.org/docs/9.5/transaction-iso.html The SQL standard defines four levels of transaction isolation. The most strict is Serializable, which is defined by the standard in a paragraph which says that any concurrent execution of a set of Serializable transactions is guaranteed to produce the same effect as running them one at a time in some order. The other three levels are defined in terms of phenomena, resulting from interaction between concurrent transactions, which must not occur at each level. The standard notes that due to the definition of Serializable, none of these phenomena are possible at that level. (This is hardly surprising -- if the effect of the transactions must be consistent with having been run one at a time, how could you see any phenomena caused by interactions?) The phenomena which are prohibited at various levels are: dirty read A transaction reads data written by a concurrent uncommitted transaction. nonrepeatable read A transact...