事务查看开始后对数据库行所做的更改
Transaction viewing changes to database rows made after it began
我发现了一些在使用 Postgres 和 Go GORM 时无法解决的问题。
在下面的代码片段中,我尝试查找、删除和创建相同的项目,但是在 2 个不同的事务中,但是我开始第二个事务并在提交第一个事务之前找到项目。
在第二笔交易中,发生了一些奇怪的事情。当我调用 t2.Delete()
时,它不会删除任何行。在数据库中,调用t1.Commit()
后,item的主键变成了2
,但是由于已经得到了旧的item,所以对主键1
.[=18=进行了删除]
为什么事务 2 看到新行,尽管它是在提交第一个之前创建的?
db, err := gorm.Open("postgres", "host=localHost port=5432 user=postgres dbname=test password=postgres sslmode=disable")
defer db.Close()
db.DropTableIfExists(&Product{})
db.AutoMigrate(&Product{})
db.LogMode(true)
db.Create(&Product{Code: "A", Price: 1000})
// SQL: INSERT INTO "products" ("code","price") VALUES ('A',1000) RETURNING "products"."id"
// Start transaction 1, find item, delete it
t1 := db.Begin()
product := &Product{}
err = t1.Find(product, "code = ?", "A").Error
// SQL: SELECT * FROM "products" WHERE (code = 'A')
err = t1.Delete(product).Error
// SQL: DELETE FROM "products" WHERE "products"."id" = 1
// Start transaction 2 and get item, before transaction 1 creates new item and commits
t2 := db.Begin()
product2 := &Product{}
err = t2.Find(product2, "code = ?", "A").Error
// SQL: SELECT * FROM "products" WHERE (code = 'A')
err = t1.Create(&Product{Code: "A", Price: 3000}).Error
// SQL: INSERT INTO "products" ("code","price") VALUES ('A',3000) RETURNING "products"."id"
err = t1.Commit().Error
// Database now contains
err = t2.Delete(product2).Error
// SQL: DELETE FROM "products" WHERE "products"."id" = 1
// [0 rows affected or returned ]
err = t2.Save(&Product{Code: "A", Price: 4000}).Error
// SQL: INSERT INTO "products" ("code","price") VALUES ('A',4000) RETURNING "products"."id"
// ERROR HERE: pq: duplicate key value violates unique constraint "products_code_key"
err = t2.Commit().Error
注意: GORM 默认使用 Read Committed isolation level
。如果它正常工作,我知道这会导致完整性问题。我将改为使用 Serializable
,如果指定 "FOR UPDATE;"
,这会在 Commit 时出错,或者在 Get 时阻塞
来自 PostgreSQL docs
Read Committed is the default isolation level in PostgreSQL. When a
transaction uses this isolation level, a SELECT query (without a FOR
UPDATE/SHARE clause) sees only data committed before the query began;
it never sees either uncommitted data or changes committed during
query execution by concurrent transactions. In effect, a SELECT query
sees a snapshot of the database as of the instant the query begins to
run.
...
Also note that two successive SELECT commands can see different data,
even though they are within a single transaction, if other
transactions commit changes after the first SELECT starts and before
the second SELECT starts.
第二段似乎概括了你的情况。在 t1
提交后启动的 t2
中的任何 "SELECT" 都可以看到 t1
应用的更新。
我发现了一些在使用 Postgres 和 Go GORM 时无法解决的问题。
在下面的代码片段中,我尝试查找、删除和创建相同的项目,但是在 2 个不同的事务中,但是我开始第二个事务并在提交第一个事务之前找到项目。
在第二笔交易中,发生了一些奇怪的事情。当我调用 t2.Delete()
时,它不会删除任何行。在数据库中,调用t1.Commit()
后,item的主键变成了2
,但是由于已经得到了旧的item,所以对主键1
.[=18=进行了删除]
为什么事务 2 看到新行,尽管它是在提交第一个之前创建的?
db, err := gorm.Open("postgres", "host=localHost port=5432 user=postgres dbname=test password=postgres sslmode=disable")
defer db.Close()
db.DropTableIfExists(&Product{})
db.AutoMigrate(&Product{})
db.LogMode(true)
db.Create(&Product{Code: "A", Price: 1000})
// SQL: INSERT INTO "products" ("code","price") VALUES ('A',1000) RETURNING "products"."id"
// Start transaction 1, find item, delete it
t1 := db.Begin()
product := &Product{}
err = t1.Find(product, "code = ?", "A").Error
// SQL: SELECT * FROM "products" WHERE (code = 'A')
err = t1.Delete(product).Error
// SQL: DELETE FROM "products" WHERE "products"."id" = 1
// Start transaction 2 and get item, before transaction 1 creates new item and commits
t2 := db.Begin()
product2 := &Product{}
err = t2.Find(product2, "code = ?", "A").Error
// SQL: SELECT * FROM "products" WHERE (code = 'A')
err = t1.Create(&Product{Code: "A", Price: 3000}).Error
// SQL: INSERT INTO "products" ("code","price") VALUES ('A',3000) RETURNING "products"."id"
err = t1.Commit().Error
// Database now contains
err = t2.Delete(product2).Error
// SQL: DELETE FROM "products" WHERE "products"."id" = 1
// [0 rows affected or returned ]
err = t2.Save(&Product{Code: "A", Price: 4000}).Error
// SQL: INSERT INTO "products" ("code","price") VALUES ('A',4000) RETURNING "products"."id"
// ERROR HERE: pq: duplicate key value violates unique constraint "products_code_key"
err = t2.Commit().Error
注意: GORM 默认使用 Read Committed isolation level
。如果它正常工作,我知道这会导致完整性问题。我将改为使用 Serializable
,如果指定 "FOR UPDATE;"
来自 PostgreSQL docs
Read Committed is the default isolation level in PostgreSQL. When a transaction uses this isolation level, a SELECT query (without a FOR UPDATE/SHARE clause) sees only data committed before the query began; it never sees either uncommitted data or changes committed during query execution by concurrent transactions. In effect, a SELECT query sees a snapshot of the database as of the instant the query begins to run.
...
Also note that two successive SELECT commands can see different data, even though they are within a single transaction, if other transactions commit changes after the first SELECT starts and before the second SELECT starts.
第二段似乎概括了你的情况。在 t1
提交后启动的 t2
中的任何 "SELECT" 都可以看到 t1
应用的更新。