How to handle Objectivity exceptions - Java

Click here to view in C++

You can put try catch blocks around Objectivity code to catch any exceptions thrown by Objectivity.

try
{
...
}
catch(Exception e)
{
	e.printStackTrace();
	session.abort();
//it is best practice to abort any transactions before returning from a failed app
	return 1;
}

Here is an example of using Objectivity transactions within a try catch block:

simpleapp.java


import com.objy.db.*;
import com.objy.db.app.*;

public class simpleapp
{
	public static void main(String[] args)
	{
		String bootfile = "test.boot";		//insert bootfile name here
		String dbName = "testDB";		//insert db name here
		String contName = "testCont";		//insert cont name here

		Connection connection = null;
		Session session = null;
		try
		{
			connection = Connection.open(bootfile, oo.openReadWrite);
			connection.loadSchemaClasses(true);
			session = new Session();
			ooFDObj fd = session.getFD();
			session.begin();
			session.commit();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			session.abort();
		}
	}
}

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 lockconflict, abort, and try again.