Connection Pooling?
Hi all I am new to connection pooling java.
I searched in the google found some examples.
Here are the examples
import java.util.*;
import java.sql.*;
publicclass ConnectionPoolimplements Runnable
{
// Number of initial connections to make.
privatestaticint m_InitialConnectionCount = 5;
// A list of available connections for use.
privatestatic Vector m_AvailableConnections =new Vector();
// A list of connections being used currently.
privatestatic Vector m_UsedConnections =new Vector();
// The URL string used to connect to the database
privatestatic String m_URLString =null;
// The username used to connect to the database
privatestatic String m_UserName =null;
// The password used to connect to the database
privatestatic String m_Password =null;
// The cleanup thread
private Thread m_CleanupThread =null;
//Constructor
public ConnectionPool(String urlString, String user, String passwd)throws SQLException
{
// Initialize the required parameters
m_URLString = urlString;
m_UserName = user;
m_Password = passwd;
for(int cnt=0; cnt<m_InitialConnectionCount; cnt++)
{
// Add a new connection to the available list.
m_AvailableConnections.addElement(getConnection());
}
// Create the cleanup thread
m_CleanupThread =new Thread(this);
m_CleanupThread.start();
}
privatestatic Connection getConnection()throws SQLException
{
return DriverManager.getConnection(m_URLString, m_UserName, m_Password);
}
publicstaticsynchronized Connection checkout()throws SQLException
{
Connection newConnxn =null;
if(m_AvailableConnections.size() == 0)
{
// Im out of connections. Create one more.
newConnxn = getConnection();
// Add this connection to the "Used" list.
m_UsedConnections.addElement(newConnxn);
// We dont have to do anything else since this is
// a new connection.
}
else
{
// Connections exist !
// Get a connection object
newConnxn = (Connection)m_AvailableConnections.lastElement();
// Remove it from the available list.
m_AvailableConnections.removeElement(newConnxn);
// Add it to the used list.
m_UsedConnections.addElement(newConnxn);
}
// Either way, we should have a connection object now.
return newConnxn;
}
publicstaticsynchronizedvoid checkin(Connection c)
{
if(c !=null)
{
// Remove from used list.
m_UsedConnections.removeElement(c);
// Add to the available list
m_AvailableConnections.addElement(c);
}
}
publicstaticint availableCount()
{
return m_AvailableConnections.size();
}
publicvoid run()
{
try
{
while(true)
{
synchronized(this)
{
while(m_AvailableConnections.size() > m_InitialConnectionCount)
{
// Clean up extra available connections.
Connection c = (Connection)m_AvailableConnections.lastElement();
m_AvailableConnections.removeElement(c);
// Close the connection to the database.
c.close();
}
// Clean up is done
}
System.out.println("CLEANUP : Available Connections : " + availableCount());
// Now sleep for 1 minute
Thread.sleep(60000 * 1);
}
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
--
[code]
/Example code using ConnectionPool.
import java.sql.*;
publicclass Main
{
publicstaticvoid main (String[] args)
{
try
{
//Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
//Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
}
catch (Exception E)
{
System.err.println("Unable to load driver.");
E.printStackTrace();
}
try
{
ConnectionPool cp =new ConnectionPool("jdbc:db2:testdata","user","pass");
// Connection []connArr = new Connection[7];
//
// for(int i=0; i<connArr.length;i++)
// {
// connArr[i] = ConnectionPool.checkout();
// System.out.println("Checking out..." + connArr[i]);
// System.out.println("Available Connections ... " + cp.availableCount());
// }
//
// for(int i=0; i><connArr.length;i++)
// {
// ConnectionPool.checkin(connArr[i]);
// System.out.println("Checked in..." + connArr[i]);
// System.out.println("Available Connections ... " + cp.availableCount());
// }
Connection con = ConnectionPool.checkout();
System.out.println("Checking out..." + con);
System.out.println("Available Connections ... " + ConnectionPool.availableCount());
Connection con1 = ConnectionPool.checkout();
System.out.println("Checking out..." + con1);
System.out.println("Available Connections ... " + ConnectionPool.availableCount());
ConnectionPool.checkin(con);
ConnectionPool.checkin(con1);
Connection con2 = ConnectionPool.checkout();
System.out.println("Checking out..." + con2);
System.out.println("Available Connections ... " + ConnectionPool.availableCount());
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
everything looks clear to me.
but I have one question, are these are the connectionpooling examples.
because I saw some where connection pooling support only JDK1.4.1 and above, but the above example works with lower versions also..
Can anyone clear my doubt and if these are not real connection pooling examples please can provide example.
I need it for stand alone application with multithreading.
thanks>

