读已提交和幻读(Spring)
当我设置了读已提交隔离级别并且在事务中进行幻读时会发生什么情况。它会回滚并重新运行还是只是回滚?
What happens when I have a Read Committed isolation level set and a phantom read is made in my transaction. Does it get rolled-back and re-run or just rolled-back?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除非我对提交读隔离的理解不太可靠(总是有可能),否则提交读的全部意义在于您看不到幻像行 - 被另一个事务更改的行,但更改从未提交并随后回滚。因此,在应用程序级别,通过提交读隔离,应用程序看不到幻象。
DBMS 内部执行的操作取决于 DBMS。在 MVCC 系统中,您的事务将仅读取事务启动时提交的版本。在锁定系统中,您可能会碰到其他事务的锁,然后您自己的事务可能会跳过或阻塞。但是,据我所知,DBMS 不会因为该问题而回滚您的事务。
Unless my understanding of Committed Read isolation is wonky (always a possibility), then the whole point of Committed Read is that you cannot see Phantom Rows - rows that were changed by another transaction but the change was never committed and later rolled back. So, at the application level, with Committed Read isolation, the application cannot see phantoms.
What the DBMS does internally depends on the DBMS. In a MVCC system, your transaction will simply read the version committed at the time your transaction started. In a locking system, you might hit the other transaction's lock, and then your own might skip or block. However, AFAIK, the DBMS does not roll back your transaction because of the problem.
不,它不会导致任何东西回滚。发生的情况是您的代码看到错误的数据,并可能据此做出错误的决策,仅此而已。
No, it doesn't cause anything to get rolled back. What happens is your code sees bad data, and probably makes bad decisions based on that, that's all.
您可能会看到过时的数据(仅当第二次读取时,该行位于具有更新列的表中)。为了避免此类导致数据损坏的情况,请使用乐观锁定。
You might see stale data (only if on the second read, that row was in that table with updated columns). To spare yourself from such things making your data go bad, use optimistic locking.