[jboss-user] [JBoss Cache Users] - how to lock a node for update?
alllle
do-not-reply at jboss.com
Mon Sep 21 18:24:02 EDT 2009
I just started using JBoss Cache and went through the doc and searched the forum but didn't find the answer for my question. I am using JBossCache 3.2.0.GA.
I need to perform the following operation frequently in a clustered caching environment:
| Node node = cache.getChild(Fqn.fromString("app/sequence/msgId"));
| Integer currentId = (Integer)node.get("id");
| Integer nextId = new Integer(currentId.intValue()+1);
| node.put("id", nextId);
|
Basically, a read followed by a write that increment the value. I'd like to know what's the best way to approach this.
If I don't use any locking, the node.put() will, according to the doc, fast fail if another thread on the same JBoss node or the clustered node updates the value, in which case I need to capture exception and repeat this operation and might suffer the starvation problem.
Alternatively, I think I can use LockManager.lock() to proactively lock the node before read / update it. The starvation problem may still occur, but if I obtain the lock, I know I won't get an exception for writing later one. But I didn't find any useful document on the use of the LockManager and therefore, I am not sure if this approach is recommended or not.
Furthermore, I am not sure how my setting of the isolationLevel attribute (either "READ_COMMITTED" or "REPEATABLE_READ") affects the behavior in the above two scenarios.
Can someone please share your thoughts on this?
Thank you,
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4256268#4256268
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4256268
More information about the jboss-user
mailing list