doudi5291 2019-05-28 07:53
浏览 107
已采纳

Golang Postgres可重复读隔离级别不起作用

My understanding of the REPEATABLE READ transaction isolation level is that once it is set, the data read through in the transaction will not change. I have used the following code to verify this:

ctx = context.Background()
tx, _ := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable}) 
time.Sleep(5 * time.Second)
// do query on tx

While the process is sleeping, I have inserted another record through the console. But the newly inserted record has appeared in the results. Why is that so? I don't want to read the records which are inserted after the transaction has begun. I also tried:

tx, _ := db.Begin()
_, err = tx.Exec(`set transaction isolation level repeatable read;`)

But still the results are the same.

  • 写回答

1条回答 默认 最新

  • dongshuofu0039 2019-05-28 07:58
    关注

    The snapshot of a REPEATABLE READ or SERIALIZABLE transaction is not taken at the time when the transaction is started, but when the first SQL statement runs inside the transaction.

    First, this is an optimization for the case that the transaction does not run any statement at all. Second, it is the only way to allow setting the transaction isolation level after the start of the transaction, but before any SQL statement has been run with

    SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么