Posts

Showing posts with the label lost update

How to avoid LOST UPDATE in database

A lost update will happen when there are many transactions which concurrently modify on one column This is one transaction that update amount for the person which has id = 1 func updatePersonWith ( db * sqlx . DB , ctx context . Context , extra int ) { context := ctx tx , err := db . BeginTx ( context , & sql . TxOptions { Isolation : sql .LevelDefault}) if err != nil { log . Infof ( "update1 Create tx fail %v" , err ) } rows , err := tx . Query ( `select amount from person where id=$1` , 1 ) if err != nil { log . Infof ( "Exec sql failed %v" , err ) } var amount int for rows . Next () { if err := rows . Err (); err != nil { log . Infof ( "Exect sql get amount1 failed %v" , err ) } err = rows . Scan (& amount ) log . Infof ( "amount= %d" , amount ) break } rows . Close () //just for test time . Sleep ( time . Second * 3 ) amount = amount...