expire_on_commit 默认是 True,它到底在做什么?关掉它能省多少、会丢什么?这篇用真实的 PostgreSQL 和一个会改列的触发器,把两种设置下 commit 之后的对象逐项对照。
expire_on_commit=True commit 之后读 n.title 与 n.id:新发 1 条 SQL → SELECT note.id, note.title, note.ver, note.updated FROM note WHERE not expire_on_commit=False commit 之后读 n.title 与 n.id:新发 0 条 SQL
上一篇:Pool Checkout 与连接释放;下一篇:乐观锁。
expire_on_commit=True 请求结束后读 box[0].title → DetachedInstanceError expire_on_commit=False 请求结束后读 box[0].title → 读到 'x'
expire_on_commit=True 库里实际: mine|2 | 本会话对象 n.title = 'mine', n.ver = 2 expire_on_commit=False 库里实际: mine|2 | 本会话对象 n.title = 'mine', n.ver = 0
expire_on_commit=True 更新并 commit 后对象 n.ver = 1(提交前 0),库里实际 ver = 1 expire_on_commit=False 更新并 commit 后对象 n.ver = 0(提交前 0),库里实际 ver = 1
expire_on_commit=True 逐个读 title: 114.0 ms,1000 条 SQL expire_on_commit=False 逐个读 title: 0.1 ms,0 条 SQL