Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Implementing rowlocking

by ikegami (Patriarch)
on Jan 09, 2007 at 16:08 UTC ( [id://593747] : note . print w/replies, xml ) Need Help??

in reply to Implementing rowlocking

[ I see solutions. Hopefully, I can shed some light on the problem. ]

First, it's important to realize that you want to lock a record for an extended period of time, one that can span longer than a connection. (arkturuz, herveus and myself in the chat last night didn't realize this.)

There are two major problems with doing your own locking. The length of time for which you want to lock a record exacerbates both.

First, the situation where you're trying to lock something that's already locked is hard to handle. If you were to use LOCK TABLE, it would block until the lock is obtained. If you were to use your own locking mechanism, then you'd have to keep trying to lock the data. That could put a heavy load on the DB if multiple clients are trying to lock locked records.

Second, a crashed client can hold a record locked indefinitely. You could timeout locks, but that's dangerous.

That's why people (davidrw, SheridanCat) are suggesting optimistic locking. It goes along the lines of

  • Don't lock the record while viewing/editing it.
  • When it's time update the record in the database, first make sure it hasn't changed.
  • Handle conflicts automatically and/or by having the user redo his update (in part or in whole).

Replies are listed 'Best First'.
Re^2: Implementing rowlocking
by smithers (Friar) on Jan 09, 2007 at 20:45 UTC
    The optimisitic locking is a solid way to approach this. I would not devise a custom/app-specific locking strategy inside the database as you described earlier. That's just prone to more issues, complexity and maintenance.

    Regardless of Rdbms vendor locking or your custom locking, holding a lock for 5+ minutes is too long on a busy system. Seems that would lead to transaction blocking or even deadlocking. Good luck with your project.