Deadlock handling for beginners
597600Jun 24 2008 — edited Jul 5 2008Not being the brightest person in the universe, I've spent some time trying to figure out how to handle DB_LOCK_DEADLOCK for my application.
It's a Web application using the PHP interface from Apache. The application is currently accessing the container in a read-only fashion. There is an update process, which time and again accesses the container to do some updating.
The environment is created with DB_INIT_MPOOL | DB_INIT_LOG | DB_INIT_TXN | DB_INIT_LOCK. This is a setup for transactions, as the update process uses transactions.
During updates, another writer, or another reader, may receive DB_LOCK_DEADLOCK when trying to access data that resides on a page locked by the update.
I do not want the web application to fail when accessing the container while an update is going on. So I have it catch the XmlException that results from the DB_LOCK_DEADLOCK, pause one second, and retry, up to ten times. (Then, I want it to fail.)
Now if some process holds a lock and keeps holding for 30 seconds, the web application will block on the request until the lock is released. It does not receive DB_LOCK_DEADLOCK until the locker releases its lock.
I found out that having the db_deadlock utility run with -t 1, I can trigger deadlock detection, so the web application will be sent DB_LOCK_DEADLOCK, which it can handle to pause, retry the lookup, and eventually succeed, or fail.
Could someone more experienced than I am please confirm this approach is - or is not - completely insane?
Any suggestions as to how to handle this more professionally highly appreciated.
Would the read-only web application have any benefit from opening the container using DBXML_TRANSACTIONAL in addition to DB_RDONLY?
Note that mod_db4, which is included in the source code distribution (dbxml-2.4.13/db-4.6.21/mod_db4), is probably not an option at the moment.
Michael Ludwig