3 Replies Latest reply on Mar 7, 2012 8:45 AM by Galder Zamarreño

    test if already owning lock

    Mathieu Lachance Novice

      I was wondering if there was any way to check if lock on a certain key/keys was already owned by the application.


      I know that at runtime, if lock is already own it will result in a no-op operation as stated :



      which make sense when looking at source code in :




         public Object visitLockControlCommand(TxInvocationContext ctx, LockControlCommand command) throws Throwable {

            if (!ctx.isInTxScope())

               throw new IllegalStateException("Locks should only be acquired within the scope of a transaction!");



            //first go remotely - required by DLD. Only acquire remote lock if multiple keys or the single key doesn't map

            // to the local node.

            if (ctx.isOriginLocal()) {

               final boolean isSingleKeyAndLocal = !command.multipleKeys() && cdl.localNodeIsPrimaryOwner(command.getSingleKey());

               if (!isSingleKeyAndLocal || command.multipleKeys()) {

                  LocalTransaction localTx = (LocalTransaction) ctx.getCacheTransaction();

                  if (!localTx.getAffectedKeys().containsAll(command.getKeys())) {

                     invokeNextInterceptor(ctx, command);


                  } else {

                     log.tracef("Already own locks on keys: %s, skipping remote call", command.getKeys());






      Though it would be nice to check in the application if a certain lock is already owned to do assertion of some kind.

      Is it possible to do so actually ?