Solved

DB connection pool using Hashtable class

Posted on 2001-08-08
6
592 Views
Last Modified: 2008-03-17
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
0
Comment
Question by:nhvo73
6 Comments
 
LVL 3

Expert Comment

by:black
ID: 6366644
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?
0
 
LVL 6

Accepted Solution

by:
kotan earned 300 total points
ID: 6366645
I found that there is an error in the getConnection().
How come you compare the dbURL with databasePath. You should compare it with dbConnectionTable if that url exist.
if you want to ignore case, you can change all the databasePath to lower case first
     databasePath.toLowerCase()
in DBCache() when in into hashtable. And then, compare the dbURL in lowercase in getConnection().

If it is not DbConnection pool, you can make another connection like
     cReturn = DriverManager.getConnection("jdbc:oracle:thin:@" + dbURL");
but I think you should provide the user name and password.

For the timeout and connection error, it will return a null value. Then you can throw a SQLException() when cReturn is null.


public Connection getConnection(String dbURL) throws SQLException {
     Connection cReturn = null;
     String dbURLlower = dbURL.toLowerCase();
     if (dbConnectionTable.containsKey(dbURLLower)) {
          cReturn = (Connection) dbConnectionTable.get(dbURLLower);
     } else {
          cReturn = DriverManager.getConnection("jdbc:oracle:thin:@" + dbURL");
     }

     if (cReturn == null) {
          throw new SQLException();
     }

     return cReturn;
}
0
 
LVL 3

Expert Comment

by:black
ID: 6366648
A BSD licensed connection pooling package:
http://www.bitmechanic.com/projects/jdbcpool/
might be useful.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:graf27
ID: 6367327
0
 
LVL 3

Expert Comment

by:graf27
ID: 6367553
0
 

Author Comment

by:nhvo73
ID: 6389608
I somehow got the idea from your comment to finish up the code.  Thanks a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
what is the difference between "sudo su" and "su - root" 6 104
micro services vs rest web services 16 85
Java Timer (static) 9 36
object oriented programming comparison 5 54
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now