File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Using an existing constructor question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Using an existing constructor question" Watch "Using an existing constructor question" New topic
Author

Using an existing constructor question

Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
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
Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Sorry, hit the wrong key before finishing my posting. Rewriting it again.

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 and log the information into a log file using Log4j. 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 instance. 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? i am using JDK 1.3. This is some of what I have which somewhat affects the existing class of the constructor -


Would appreciate for your expert suggestion.

Thanks!

[edit]Add code tags. CR[/edit]
[ June 20, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Not a beginner's question. Shall move this thread.

Don't call a public non-final method from the constructor. Please use CODE tags, otherwise we only see an illegible mass of code. Without knowing what you have in your dbConnect() method, I don't think we can make any sensible comments.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using an existing constructor question
 
Similar Threads
Servlet Login
isNew() method
does gc still have a chance to do its job if i kill the java process
database connections in threaded app
java.rmi.RemoteException: NullPointerException