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



Optimistic collision exception

You can receive an OptimisticCollisionException directly, or receive it with an ObjectGridException.

The following code is an example of how to catch the exception and then display its message:

try {
...
} catch (ObjectGridException oe) {
    System.out.println(oe);
}


Exception cause

OptimisticCollisionException is created in a situation in which two different clients try to update the same map entry at relatively the same time. For example, if one client attempts to commit a session and update the map entry after another client reads the data before the commit, that data is then incorrect. The exception is created when the other client attempts to commit the incorrect data.


Retrive the key that triggered the exception

It might be useful, when troubleshooting such an exception, to retrieve the key corresponding to the entry that triggered the exception. The benefit of the OptimisticCollisionException is it contains the getKey method, which returns the object representing that key. The following example demonstrates how to retrieve and print the key when catching OptimisticCollisionException:

try {
...
} catch (OptimisticCollisionException oce) {
    System.out.println(oce.getKey());
}


ObjectGridException causes an OptimisticCollisionException

OptimisticCollisionException might be the cause of ObjectGridException displaying. If this is the case, you can use the following code to determine the exception type and print out the key. The following code uses the findRootCause utility method as described in the section below.

try {
...
}
catch (ObjectGridException oe) {
    Throwable Root = findRootCause( oe );
    if (Root instanceof OptimisticCollisionException) {
        OptimisticCollisionException oce = (OptimisticCollisionException)Root;
        System.out.println(oce.getKey());
    }
}


General exception handling technique

Knowing the root cause of a Throwable object is helpful in isolating the source of an error. The following example demonstrates how an exception handler uses a utility method to find the root cause of the Throwable object.

Example:

static public Throwable findRootCause( Throwable t )
{
     // Start with Throwable that occurred as the root.
     Throwable root = t;

     // Follow cause chain until last Throwable in chain is found.
     Throwable cause = root.getCause();
     while ( cause != null )
     {
        root = cause;
        cause = root.getCause();
     }

     // Return last Throwable in the chain as the root cause.
     return root;
}


Parent topic:

Program for transactions


Related concepts

Transaction processing overview

Transaction isolation

Run parallel business logic on the data grid (DataGrid API)


Related tasks

Use locking