Of course I could simply list my complete code here, but I am here to
help and not sell my code for free. Spoonfeed-code will not help you at all, because you would not understand all the problems and solutions I went through while developing it. We are not at rentACoder.com, so it's the other way around here:
You list the code you have programmed so far, and we are here to help if you are stuck or tell you how to make it better.
As a favor, and to give you a head-start, I will tell you the concept:
A pool is a storage where you store your DB-connections. You can borrow a connection from the pool and after usage you give it back to the pool. For a single thread, there is no problem and you only have one connection at all in the pool. But for multiple threads you need more. For example the first thread borrows one, then the second thread borrows another one (the pool just creates a new one, opens it and and gives it back, because the first one is still in use). Then the first thread gives back its connection. Then a third thread borrows one (the pool just returns the connection the first thread had used before). Then the third and second thread give back the connections. (the pool ends up with 2 unused connections). That's all easy to implement, but the complicated part is the timeout: if a connection is not used for a long time or is not returned, it should be closed and the object should be destroyed. Here you have to deal with concurrent threads and synchronization.
Here is the schema:
- public class ConnectionPool extends ObjectPool
-
{
-
public Connection borrowConnection()
-
{
-
// check-out a connection from pool with: return (Connection) super.checkOut();
-
}
-
public returnConnection(Connection c)
-
{
-
// check-in a connection back into pool with: super.checkIn(c);
-
}
-
private ConnectionPool()
-
{
-
// read driverName, url, username, password from configuration file (or pass them as parameter of this constructor) and store them in local variables.
-
// register driver with: DriverManager.registerDriver(...)
-
}
-
Object create()
-
{
-
// return a new connection with DriverManager.getConnection(url, username, password)
-
}
-
boolean validate(Object o)
-
{
-
// return true/false if a connection is opened/closed
-
}
-
expire(Object o)
-
{
-
// close connection
-
}
-
}
-
Now the ObjectPool:
- public abstract class ObjectPool
-
{
-
private final Hashtable pool = new Hashtable() // stores object as key and creation time, unused-status and additional info as value
-
-
abstract Object create();
-
abstract validate(Object o);
-
abstract expire(Object o);
-
-
ObjectPool()
-
{
-
// create CleanUpThread(this);
-
// start cleanUpThread
-
}
-
public void finalize()
-
{
-
// stop cleanUpThread
-
}
-
synchronized Object checkOut()
-
{
-
// get an unused object from pool. It also should still be valid, (for example a connection that was not closed by external database), by calling validate(). If there is none, create one calling create(). Return it and mark it as used.
-
}
-
synchronized void checkIn(Object o)
-
{
-
// mark corresponding object in pool as unused.
-
}
-
synchronized void cleanUp()
-
{
-
// This function will be called by the cleanupThread. It goes through all objects inside the pool and removes the timed-out and invalid ones.
-
}
-
}
-
Now the CleanupThread:
- class CleanUpThread extends Thread
-
{
-
CleanUpThread(ObjectPool pool)
-
{
-
// continue to run even if current parent thread was detroyed, by this.setDaemon(true)
-
}
-
public void run()
-
{
-
// do in a loop until stopped: sleep for 10 seconds with sleep(1000 * 10), then wake up and call pool.cleanUp()
-
}
-
-
}
-