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>

[11653 byte] By [nim_ramesha] at [2007-11-26 19:43:40]
# 1
> I need it for stand alone application with multithreading.You should consider using [url= http://jakarta.apache.org/commons/dbcp/]Jakarta DBCP[/url]. No sense reinventing the wheel, and all.~
yawmarka at 2007-7-9 22:27:05 > top of Java-index,Java Essentials,Java Programming...
# 2
>No sense reinventing the wheel, and all.Wasn't that the advice in your previous thread: http://forum.java.sun.com/thread.jspa?threadID=5140525&messageID=9517076Why didn't you listen?
DrLaszloJamfa at 2007-7-9 22:27:05 > top of Java-index,Java Essentials,Java Programming...
# 3
I really don't understand the examples of previous post..So i searched in the google.. I found above examples..But both are looks different for me..lot of confusion.Which one I have to use?
nim_ramesha at 2007-7-9 22:27:05 > top of Java-index,Java Essentials,Java Programming...
# 4
Both DBCP and C3P0 are widely used. I'm not sure what you are asking,but I would try either of them before resorting to writing my own connection pool.
DrLaszloJamfa at 2007-7-9 22:27:05 > top of Java-index,Java Essentials,Java Programming...
# 5
My question isCan i use these (DBCP and C3P0 ) for my standalone application?Thanks
nim_ramesha at 2007-7-9 22:27:05 > top of Java-index,Java Essentials,Java Programming...
# 6
> Can i use these (DBCP and C3P0 ) for my standalone application?Yes. I've used them both in standalone applications, via Hibernate.
DrLaszloJamfa at 2007-7-9 22:27:05 > top of Java-index,Java Essentials,Java Programming...
# 7

Hi DrLaszloJamf,

I am using the DBCP. I looked in to BasicDataSourceExample.java example file. I have one question

How can I get the connection?, like in the main method everytime I have to call the connection when ever I need.

And How can I send back to connection pool with using conn.close()?

can you please suggest me

thanks

nim_ramesha at 2007-7-9 22:27:05 > top of Java-index,Java Essentials,Java Programming...
# 8
> How can I get the connection?By calling getConnection, as in that example.> And How can I send back to connection pool with using conn.close()?Yes, close sends it back to the pool.
DrLaszloJamfa at 2007-7-9 22:27:05 > top of Java-index,Java Essentials,Java Programming...