Hello All,
I am trying to write a code to iterate through a properties file to get database connectivity info and check the connectivity of multiple databases. I am also doing predetermined number of attempts in case of failures to the database connectivity. I am using a Thread object's sleep method for the delay of these attempts. The existing codes uses a constructor for reading a properties file from a util.Properties object. This constructor is widely used to instantiate and use it's method to create a database connection object for a single database. But my coding effort involves multiple database access. Without affecting the existing constructor what would be the best possible solution to achieve what I am trying to do. This is what I had which somewhat affects the existing class of the constructor -
public class DBConn {
protected Connection connection = null;
private DBConn() {}
...
// This is the existing constructor. Taken out the common codes within it
// and have added it in the new dbConnect method
public DBConn(String dbInstance) throws SQLException, IOException {
// Create Properties object
Configuration aConfiguration = new Configuration();
// Now calling the method to do the actual db connection
dbConnect(dbInstance, aConfiguration);
}
public DBConn(int intMaxAttempt) throws SQLException, IOException {
// Create a Configuration object
Configuration aConfiguration = new Configuration();
Thread thread = new Thread();
// Read the Configuration.properties file and retrieve all
// valid dbInstance related to the db.pool key.
ArrayList alKeys = aConfiguration.getParamArrayList("db.pool");
// Flag and counter to control looping
boolean boolConnFlag = false;
int counter = 0;
// Now iterating thru the array list
Iterator iterator = alKeys.iterator();
while (iterator.hasNext()) {
String instanceCode = (String) iterator.next();
// Now loop thru until a successful db connection is
// established or exceeds the attempts counter
while ( (boolConnFlag == false) && (counter <= intMaxAttempt))
{
// Now calling the method to do the actual db connection
boolConnFlag = dbConnect(instanceCode, aConfiguration);
LOG.info("1 boolConnFlag -> " + boolConnFlag);
// increment the counter
// For testing purpose set boolConnFlag to false
//boolConnFlag = false;
counter++;
LOG.info("2 boolConnFlag -> " + boolConnFlag);
LOG.info("counter -> " + counter);
// Now add delay before entering the loop again
// Arbitarily chosen 300 milliseconds. Could be any valu
e
I am trying to write a code to iterate through a properties file to get database connectivity info and check the connectivity of multiple databases. I am also doing predetermined number of attempts in case of failures to the database connectivity. I am using a Thread object's sleep method for the delay of these attempts. The existing codes uses a constructor for reading a properties file from a util.Properties object. This constructor is widely used to instantiate and use it's method to create a database connection object for a single database. But my coding effort involves multiple database access. Without affecting the existing constructor what would be the best possible solution to achieve what I am trying to do. This is what I had which somewhat affects the existing class of the constructor -
public class DBConn {
protected Connection connection = null;
private DBConn() {}
...
// This is the existing constructor. Taken out the common codes within it
// and have added it in the new dbConnect method
public DBConn(String dbInstance) throws SQLException, IOException {
// Create Properties object
Configuration aConfiguration = new Configuration();
// Now calling the method to do the actual db connection
dbConnect(dbInstance, aConfiguration);
}
public DBConn(int intMaxAttempt) throws SQLException, IOException {
// Create a Configuration object
Configuration aConfiguration = new Configuration();
Thread thread = new Thread();
// Read the Configuration.properties file and retrieve all
// valid dbInstance related to the db.pool key.
ArrayList alKeys = aConfiguration.getParamArrayList("db.pool");
// Flag and counter to control looping
boolean boolConnFlag = false;
int counter = 0;
// Now iterating thru the array list
Iterator iterator = alKeys.iterator();
while (iterator.hasNext()) {
String instanceCode = (String) iterator.next();
// Now loop thru until a successful db connection is
// established or exceeds the attempts counter
while ( (boolConnFlag == false) && (counter <= intMaxAttempt))
{
// Now calling the method to do the actual db connection
boolConnFlag = dbConnect(instanceCode, aConfiguration);
LOG.info("1 boolConnFlag -> " + boolConnFlag);
// increment the counter
// For testing purpose set boolConnFlag to false
//boolConnFlag = false;
counter++;
LOG.info("2 boolConnFlag -> " + boolConnFlag);
LOG.info("counter -> " + counter);
// Now add delay before entering the loop again
// Arbitarily chosen 300 milliseconds. Could be any valu
e