Database Deadlock in SELECT FOR UPDATE

回眸只為那壹抹淺笑 提交于 2019-12-03 20:18:36
Nicolai

I'm assuming that SELECT FOR UPDATE will lock the row and when other session will try to update the same row, it will wait until session 1 completes execution.

That is exactly. But you need to close transaction when you finish with this row or close session. The possible situation for your issue is the next:

Process 1 locks row with ID=2, updates it and going to the next record with ID=1 (but session and transaction is still active) Process 2 already locked row with ID=1 and going to lock row with ID=2 (but session and transaction is still active)

So Process 1 is waiting for record ID=1 and hold record ID=2

Process 2 is waiting for record ID=2 and hold record ID=1

This is a dead lock. You have to complete transaction after finished work with record to free it for other processes.

If you need several record to update in one transaction just lock them all together and free after work is finished.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!