Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DB connection pool using Hashtable class

Posted on 2001-08-08
6
Medium Priority
?
600 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 900 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

722 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