Exception Handling

What is a Objectivity/DB exception?

Objectivity/DB exceptions are instances of the exception classes that are defined by the Objectivity/DB programming interfaces. The exception classes corresponding to error conditions that can occur during the execution of an application. Your application can catch Objectivity/DB exceptions to deal appropriately with error conditions.

Why do exceptions occur?

Objectivity exceptions occur for three main reasons:

    • You have used the programming interface incorrectly. It is up to you to decide whether to catch exceptions resulting from programming errors or let the program terminate, indicating that you need to correct the program logic.
    • A function was unable to complete because some other successful operation has blocked it. For example, a function might fail to obtain a lock because another transaction already has an incompatible lock on the same object. In this case, the function would throw an ooLockConflictException.
    • A function was unable to complete because an error, such as a resource failure, occurred. For example, a function might fail to obtain a lock because a network problem prevents the application from connecting to the lock server. In this case, an ooNetworkException would be thrown.

How do I handle Objectivity/DB exceptions?

You handle exceptions as you would in any application. Enclose code that may cause exceptions in a try block and catch any relevant exceptions in the corresponding catch block. You can make use of the inheritance hierarchy of the exception classes to catch different exceptions. For example, specify ObjyRuntimeException in a catch block to catch any unchecked Objectivity for Java exception.

The example below shows the structure of a simple Objectivity for Java application. It opens a connection for read/write access to a federated database whose boot file is myFD. After performing some Objectivity/DB operations on the connected federated database, the application closes its connection.

Connection connection; // Connection object for the application
…
// Open a connection to the federated database
try {
connection = Connection.open(
"myFD", // boot file
oo.openReadWrite); // access level
}
catch (DatabaseNotFoundException exception) {
System.out.println("\nCan’t find federated database myFD.");
return;
}
catch (DatabaseOpenException exception) {
System.out.println("\nCan’t open federated database myFD.");
return;
}
… // Perform Objectivity/DB operations
// Close the connection

How do I catch a lock conflict and print out thread information?

For multithreaded/multiprocess applications, there is a chance that you may run into lock conflicts, you can use try catch blocks around Objectivity code to catch lock conflict exceptions thrown by Objectivity/DB. Enclose code that the lock conflict may occur in a try block and add the following exceptions in the corresponding catch block.

try
{
...
}catch (LockNotGrantedException ex) {
	System.out.println(threadNum + "Thread: "+ ex);
	lockConflictTrxInfo = ex.getTransactions();
        for (int i =0; i < lockConflictTrxInfo.length; i++)
        {	System.out.println(threadNum + " - "
		+ lockConflictTrxInfo[i].getHostName() + " - "
		+ lockConflictTrxInfo[i].getLockMode() + " - "
		+ lockConflictTrxInfo[i].getOID().getString() + " - " 
		+ lockConflictTrxInfo[i].getProcessID() + " - " 
		+ lockConflictTrxInfo[i].getTransactionID() + " - " 
		+ lockConflictTrxInfo[i].getUserID() + " - "
		);
	}
}

It is best practice to abort a transaction when an Objectivity exception is thrown. For multithreaded/multiprocess applications, you will sometimes, under certain circumstances, unavoidably run into lock conflicts. Most often it is avoidable by redesigning the clustering of objects, but in cases where this is not possible, you can catch the lock conflict, abort, and try again.