title: Need Code to create Connection Pool in java
body: <p>Need code to create the connection pool in java?
  How does we make sure that connection pool doesn't return the same object which is already in use?
  How happens if client closed the connection after taking it out from Connection pool?</p>
  <h3>Update 1:</h3>
  <p>I want to create this in Simple Java terms and want to see how it works in Multithreading Env. I mean which methods would be synchronized and which are not. Also will this class would be a public class? If yes then any one can access this class and reinitialize the connection pool?</p>
  <h3>Update 2:</h3>
  <p>I have some code as below. But i don't how &quot;Closing a connection coming from a pool returns it to the pool, it doesn't close the connection physically.&quot;
  Also i didn't understood this &quot;Because if a connection has been borrowed from the pool and not returned yet, it's not &quot;available&quot; and can't be redistributed to another client of the pool.&quot;</p>
  <pre><code>import java.util.*;
  import java.sql.*;
  
  class ConnectionPoolManager
  {
  
   String databaseUrl = &quot;jdbc:mysql://localhost:3306/myDatabase&quot;;
   String userName = &quot;userName&quot;;
   String password = &quot;userPass&quot;;
  
   Vector connectionPool = new Vector();
  
   public ConnectionPoolManager()
   {
    initialize();
   }
  
   public ConnectionPoolManager(
    //String databaseName,
    String databaseUrl,
    String userName,
    String password
    )
   {
    this.databaseUrl = databaseUrl;
    this.userName = userName;
    this.password = password;
    initialize();
   }
  
   private void initialize()
   {
    //Here we can initialize all the information that we need
    initializeConnectionPool();
   }
  
   private void initializeConnectionPool()
   {
    while(!checkIfConnectionPoolIsFull())
    {
     System.out.println(&quot;Connection Pool is NOT full. Proceeding with adding new connections&quot;);
     //Adding new connection instance until the pool is full
     connectionPool.addElement(createNewConnectionForPool());
    }
    System.out.println(&quot;Connection Pool is full.&quot;);
   }
  
   private synchronized boolean checkIfConnectionPoolIsFull()
   {
    final int MAX_POOL_SIZE = 5;
  
    //Check if the pool size
    if(connectionPool.size() &lt; 5)
    {
     return false;
    }
  
    return true;
   }
  
   //Creating a connection
   private Connection createNewConnectionForPool()
   {
    Connection connection = null;
  
    try
    {
     Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
     connection = DriverManager.getConnection(databaseUrl, userName, password);
     System.out.println(&quot;Connection: &quot;+connection);
    }
    catch(SQLException sqle)
    {
     System.err.println(&quot;SQLException: &quot;+sqle);
     return null;
    }
    catch(ClassNotFoundException cnfe)
    {
     System.err.println(&quot;ClassNotFoundException: &quot;+cnfe);
     return null;
    }
  
    return connection;
   }
  
   public synchronized Connection getConnectionFromPool()
   {
    Connection connection = null;
  
    //Check if there is a connection available. There are times when all the connections in the pool may be used up
    if(connectionPool.size() &gt; 0)
    {
     connection = (Connection) connectionPool.firstElement();
     connectionPool.removeElementAt(0);
    }
    //Giving away the connection from the connection pool
    return connection;
   }
  
   public synchronized void returnConnectionToPool(Connection connection)
   {
    //Adding the connection from the client back to the connection pool
    connectionPool.addElement(connection);
   }
  
   public static void main(String args[])
   {
    ConnectionPoolManager ConnectionPoolManager = new ConnectionPoolManager();
   }
  
  }
  </code></pre>