We use the same pattern for pessimistic locking. Our implementation requires an 
extra timestamp column on the table with the row you'd like to lock. We also 
have a column for the ID of the user who is locking the row for user feedback. 

All access to this table (at least the lock columns) must be through an entity 
bean. Once the entity bean is active in a transaction, you are guaranteed it is 
not active in any other transactions, unless you mess up the concurrency 
modifiers for the container (be sure to test this). This allows you to perform 
an atomic test-and-set operation on the locking row. If the timestamp is null 
or expired, go ahead and set it for some time in the future (e.g. 20 minutes) 
granting that user the exclusive lock. Otherwise, fail and report back to the 
user that the row is in use (optionally, report who owns the lock). There are 
no singletons or central lock manager so you'll have all the scalability you 
want.

You'll also want some way of releasing a lock before the expiration time, and 
we also allow a user to extend their exclusive access. We code defensively on 
the server side to be sure client applications can never extend or release a 
lock they don't actually own. It's not trivial, but it works well. 

The container's contract that an entity can only be active in a single 
transaction is the key. Otherwise, you'll end up with a race condition and 
start granting locks to multiple users in rare (and not easily reproducible) 
cases.

A word of caution: if you access your entity bean outside a transaction, which 
I'm sure you would never do, but if you did, then you will not get the atomic 
behavior you're looking for. To guaranteed the entity is always accessed inside 
a transaction use trans-attribute=Mandatory in the ejb-jar.xml descriptor. 

Good luck!


View the original post : 
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3863656#3863656

Reply to the post : 
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3863656


-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl
_______________________________________________
JBoss-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to