Comments on: How to notify event listeners in MySQL http://www.xaprb.com/blog/2007/08/29/how-to-notify-event-listeners-in-mysql/ Stay curious! Thu, 02 May 2013 12:36:53 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Xaprb http://www.xaprb.com/blog/2007/08/29/how-to-notify-event-listeners-in-mysql/#comment-19588 Xaprb Sun, 21 Aug 2011 14:30:06 +0000 http://www.xaprb.com/blog/2007/08/29/how-to-notify-event-listeners-in-mysql/#comment-19588 Thang, it’s definitely not an elegant solution, it’s a kludge at best. I wish MySQL had PostgreSQL’s feature set for these purposes.

These days I wouldn’t build this in the database. I’d build it with an external queueing system, or with Gearman.

]]>
By: Thang http://www.xaprb.com/blog/2007/08/29/how-to-notify-event-listeners-in-mysql/#comment-19585 Thang Sat, 20 Aug 2011 22:43:50 +0000 http://www.xaprb.com/blog/2007/08/29/how-to-notify-event-listeners-in-mysql/#comment-19585 sorry, you are right. there is an exact use if you want to lock a lock, then wait for awhile and have another thread wait.

the problem is that this exact use is limited for the reasons you mentioned. and all the issues that come with the produce consumer problem (coke machine – http://www.cs.illinois.edu/class/sp11/cs423/lectures/11-reader-writer-2×3.pdf, spinning, etc. are not solved).

to develop this lock into a real produce-consumer solution, you would need to use the lock to build a condition. finally, use the condition to signal arrival of items. even if you manage to do this, you would have a problem of spuriously waking up consumers (for one producer-multiple consumer). the list of short-coming continues on and on…

one practical use case is this, i want to wait for new items in a table. i don’t want to spin and i don’t want to block if there are new items. these are the 2 primary requirements for produce-consumer solution.

for example, a new select statement:

“select * from mytable where id>10 [but don't return until the result set is unempty];”

how would you go about doing this with a lock?

]]>
By: Thang http://www.xaprb.com/blog/2007/08/29/how-to-notify-event-listeners-in-mysql/#comment-19584 Thang Sat, 20 Aug 2011 22:25:16 +0000 http://www.xaprb.com/blog/2007/08/29/how-to-notify-event-listeners-in-mysql/#comment-19584 The locks used as in the code sample you have isn’t signaling anything. The threads don’t wake up when an item arrives, and they still spin. It defeats the whole purpose.

What happens if you remove the lock and release code? The behavior is exactly the same. Your code would run a little faster and waste fewer CPU cycles.

]]>
By: Xaprb http://www.xaprb.com/blog/2007/08/29/how-to-notify-event-listeners-in-mysql/#comment-19583 Xaprb Sat, 20 Aug 2011 21:28:32 +0000 http://www.xaprb.com/blog/2007/08/29/how-to-notify-event-listeners-in-mysql/#comment-19583 You’re missing the point. The lock isn’t about atomicity, it’s about signaling.

]]>
By: Thang http://www.xaprb.com/blog/2007/08/29/how-to-notify-event-listeners-in-mysql/#comment-19579 Thang Sat, 20 Aug 2011 13:08:44 +0000 http://www.xaprb.com/blog/2007/08/29/how-to-notify-event-listeners-in-mysql/#comment-19579 The lock as used in the post is useless. SELECT and INSERT commands are naturally atomic. There is absolutely no need for locks if one wants to select and insert in such a manner. The lock and release code can be removed without any adverse effect.

]]>