Link to home
Start Free TrialLog in
Avatar of nhvo73
nhvo73

asked on

DB connection pool using Hashtable class

Hi,

I am working on a project to build a DBConnection pool (using Hashtable(key,object)) for many database servers to use in many servlets.  I am somehow stuck at the method getConnection(), where it will return a conn for the servlet.  Basically, this method will return a connection if it is available in the pool; otherwise, it will create an instance right away and return it in the servlet.  If there is a timeout or a connection error, it will throw an exception.  

Below is my code.  I created an array in every servlet that contains the dataURLs for all servers and pass it in the object here.

Please let me know how I can fix it.  Thanks so much.

import java.util.*;
import java.sql.*;
import oracle.sql.*;

public class DBCache {

  Hashtable dbConnectionTable = new Hashtable();
  String databaseUserID = null;
  String databasePw = null;
  String databasePath = null;
  Driver d = null;

  public DBCache (String dbUID, String dbPW,
                  String dbURL []) {
    for (int i=0;i<=10;i++) {
     databaseUserID = dbUID;
     databasePw = dbPW;
     databasePath = dbURL[i];
     try
     {
      final Properties prop = new Properties();
      prop.setProperty("user", databaseUserID);
      prop.setProperty("password", databasePw);
      d = (Driver)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
      dbConnectionTable.put(databasePath,d.connect("jdbc:oracle:thin:@"+databasePath, prop));
     }
     catch(Exception se)
     {
      se.printStackTrace();
     }
    } //for loop

  }//end of DBCache Constructor

  public Connection getConnection(String dbURL) throws  
          SQLException {

   Connection cReturn = null;
    try {
      if (databasePath.equalsIgnoreCase(dbURL)) {
        cReturn = (Connection)dbConnectionTable.get
                  (databasePath);
      }
      else {
       ?????????
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return cReturn;
  }
 } //end of DBCache class
Avatar of black
black

I'd suggest you look at using Datasource's and third party connection pooling to do this. They are pretty standard now and provide all this functionality. Which servlet engine are you using, maybe it already provides this for you. It's quite hard to write a good connection pooling package, and I can assure you that the above class will grow extremely complex once you try to make it threadsafe, ensure connections aren't been held, make sure that they closed properly etc...
There are a lot of connection pooling package and pretty much every application server provides one, it's best to use them rather than reinventing the wheel. Also they have Datasources, rather than Connections which is what you are using.

Is there a particular reason for writing one?
ASKER CERTIFIED SOLUTION
Avatar of kotan
kotan
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A BSD licensed connection pooling package:
http://www.bitmechanic.com/projects/jdbcpool/
might be useful.
Avatar of nhvo73

ASKER

I somehow got the idea from your comment to finish up the code.  Thanks a lot.