Program guide > Access data with client applications > Programming for transactions > Use locking

Implement exception handling in locking scenarios

To prevent locks from being held for excessive amounts of time when a LockTimeoutException exception or a LockDeadlockException exception occurs, an application must ensure that it catches unexpected exceptions and calls the rollback method when something unexpected occurs.


  1. Catch the exception, and display resulting message.

    try {
    } catch (ObjectGridException oe) {

    The following exception displays as a result: Message

    This message represents the string that is passed as a parameter when the exception is created and thrown.

  2. Roll back the transaction after an exception:

    Session sess = ...;
    ObjectMap person = sess.getMap("PERSON");
    boolean activeTran = false;
        activeTran = true;
        Person p = (IPerson)person.get("Lynn");
        // Lynn had a birthday, so we make her 1 year older.
        p.setAge( p.getAge() + 1 );
        person.put( "Lynn", p );
        activeTran = false;
        if ( activeTran ) sess.rollback();

    The finally block in the snippet of code ensures that a transaction is rolled back when an unexpected exception occurs. It not only handles a LockDeadlockException exception, but any other unexpected exception that might occur. The finally block handles the case where an exception occurs during a commit method invocation. This example is not the only way to deal with unexpected exceptions, and there might be cases where an application wants to catch some of the unexpected exceptions that can occur and display one of its application exceptions. You can add catch blocks as appropriate, but the application must ensure that the snippet of code does not exit without completing the transaction.

Parent topic:

Use locking

Related concepts


Locking performance best practices

Map entry locks with query and indexes

Related tasks

Configure a locking strategy

Configure the lock timeout value

Troubleshoot deadlocks


Search Tips   |   Advanced Search