0 Replies Latest reply on Mar 31, 2005 4:35 PM by fredatwork

    Row-level locking and the java.util.concurrent Tiger package

    fredatwork Newbie

      Hello,

      I'm looking for a way to try to lock on given database row in order to implement some locking mechanism for the management of my transactions.

      For this purpose, the immediate thought is to use the following class level XDoclet tag on my entity bean :

      @jboss.persistence
       row-locking="true"

      This permits to have select statements being generated with the SELECT ... FOR UPDATE syntax, and thus generate a lock an a database row (as well as on the associated entity bean) until the current transaction is over.

      The problem is that there is (as far as I know) no way to back off from an attempt to acquire such a lock that is already held, or to give up after waiting for a specified period of time, or to cancel a lock attempt after an interrupt.

      The Java 5.0 SDK introduces a new java.util.concurrent package for concurrency matters. The ReentrantLock class has a tryLock() method that can acquire the lock if it is not held by another thread within the given waiting time. Thus the lock does not block indefinitely. See http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/ReentrantLock.html#tryLock(long,%20java.util.concurrent.TimeUnit)

      I'm wondering if there is a way to use such a new lock with CMP beans.

      Can anyone advise me how to implement row-level locking "within a given waiting time" ?

      Fred