|
There are two possible types of connections to the H-Sphere database:
IMPORTANT:
In any case, you should use only prepared stataments when working with the H-Sphere database!
Regular Shared Connections
The part of the code which establishes and uses a regular database
connection should look something similar to this:
PreparedStatement ps = null;
Connection con = Session.getDb();
try {
ps = con.prepareStatement(...........);
......................................
} finally {
Session.closeStatement(ps);
con.close();
}
Connections With Opened Transaction
In general, the part of the code which establishes and uses a transaction connection
should look as follows:
// checking if the transaction exists
boolean wasTrans = Session.isTransConnection();
Connection con = wasTrans ? Session.getDb() : Session.getTransConnection();
try {
// some operation with the database
} catch (Exception ex){
if (!wasTrans) {
// rollback transaction
con.rollback();
// here should also be some data to release cache and to synchronize with old data in HS DB
}
throw ex;
} finally {
if (!wasTrans) {
// commit transaction and release connection
Session.commitTransConnection(con);
} else {
con.close();
}
}
Important:
There is the pool of transaction connections (5 by default).
Opening a new transaction that exceeds this maximum would cause the system to hang up.
As the number of available transactions is resctricted,
you must release the transaction connection you have opened by using the following command:
Session.commitTransConnection(con);
To get an opened transaction connection:
con = Session.getTransConnection();
Later in the code, you may get the same connection:
con = Session.getDb();
Never close a transaction connection you haven't opened!
Please make sure you synchronize your objects with the corresponding data in the database.
You must release your cache if you make a rollback.
|