To prevent locks from being held for excessive amounts of time when a
LockTimeoutException exception or a LockDeadlockException exception occurs, your application must
catch unexpected exceptions and call the rollback method when an unexpected event
occurs.
Procedure
- Catch the exception, and display resulting message.
try {
...
} catch (ObjectGridException oe) {
System.out.println(oe);
}
The
following exception displays as a
result:
com.ibm.websphere.objectgrid.plugins.LockDeadlockException: Message
This
message represents the string that is passed as a parameter when the exception is created and
thrown.
- Roll back the transaction after an
exception:
Session sess = ...;
ObjectMap person = sess.getMap("PERSON");
boolean activeTran = false;
try
{
sess.begin();
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 );
sess.commit();
activeTran = false;
}
finally
{
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.