Solved

DB connection pool using Hashtable class

Posted on 2001-08-08
6
590 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
Comment Utility
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
Comment Utility
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
Comment Utility
A BSD licensed connection pooling package:
http://www.bitmechanic.com/projects/jdbcpool/
might be useful.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:graf27
Comment Utility
0
 
LVL 3

Expert Comment

by:graf27
Comment Utility
0
 

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

744 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

11 Experts available now in Live!

Get 1:1 Help Now