Click here to view in Java You can put try catch blocks around Objectivity code to catch any exceptions thrown by Objectivity.
try
{
...
}
catch(ooException &error)
{
cerr << error.what() << endl;
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: exception.cpp
#include <iostream>
using namespace std;
#include <ooObjy.h>
int main(int argc, char* argv[])
{
int returnval = 0;
//ooObjy::setLoggingOptions(oocLogAll, oocTrue, oocFalse); //enable session logging for debug
ooObjy::startup();
char *bootfile = "test.boot"; //insert bootfile name here
char *dbName = "testDB"; //insert database name here
char *contName = "testCont"; //insert container name here
ooConnection *connection = ooObjy::getConnection(bootfile);
ooSession *session = connection->createSession("main_session"); //insert session name for log or '0' for default
try
{
session->begin(oocUpdate);
ooHandle(ooFDObj) fd;
fd.exist(bootfile, oocUpdate); //open in update mode
session->commit();
}
catch(ooException &error)
{
cerr << error.what() << endl;
session->abort();
returnval = 1;
}
catch(...)
{
cerr << "unknown error" << endl;
session->abort();
returnval = 1;
}
ooObjy::shutdown();
return returnval;
}
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. |
|||
