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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大