Getting coonection from connection pool

Discussions

General J2EE: Getting coonection from connection pool

  1. Hi all,

    I wanted to get a database connection from a connection pool named "testPool". I do not want to use a datasource to do a lookup and then get the connection. I just wanted the code thru which I can directly get connections from my connection pool in my java code.

    Do anyone have any ised or the price of code?

    Thanks in Advance.
    Sabyasachi

    Threaded Messages (3)

  2. Hi all,I wanted to get a database connection from a connection pool named "testPool". I do not want to use a datasource to do a lookup and then get the connection. I just wanted the code thru which I can directly get connections from my connection pool in my java code.Do anyone have any ised or the price of code?Thanks in Advance.Sabyasachi

    there are a bunch of open source ones , i've used C3PO in my last project and it worked...just don't right one yourself..

    http://tom.tharrisx.homedns.org/javalib/c3p0-0.8.4.5/doc/

    sample code is there.
  3. A connection Pool[ Go to top ]

    This is my personal connection pool, it works, and it is very simple and customizable.. no external services or library are called here:



    package utility.db;
    import java.util.*;
    import java.sql.*;

    public class ConnectionPool
    {
      private static ConnectionPool connectionPool = null;
    //the only static instance
      private static int ConnectionLimit;
    //Contemporaeny connections limit
      private static int NumberConnections;
    //Number of opened connections
      private Vector freeConnections;
      private String dbUrl; // DB name
      private String dbDriver; // DB driver

      // Constructor
      private ConnectionPool()
      {
        try
        {
         NumberConnections=0;
         ConnectionLimit=300;
         freeConnections = new Vector();
         dbUrl = "JDBC:ODBC:Abitazioni"; //DB Url
         dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; // Driver per il database
        }
        catch(Exception e)
        {
         System.out.println("Errore in fase di caricamento dei driver dell'interfaccia DB");
        }
      }


    public static synchronized ConnectionPool getConnectionPool()
      {
         if(connectionPool == null)
           connectionPool = new ConnectionPool();
         return connectionPool;
      }
      
     public synchronized Connection getConnection()
      {
        Connection con;
         System.out.println("***\nConnection Pool Info:\nConnessioni attive:"+(NumberConnections-freeConnections.size())+" Connessioni Libere:"+freeConnections.size()+" Connessioni Disponibili:"+(ConnectionLimit-NumberConnections)+"\n***");
        if ((NumberConnections-freeConnections.size())<ConnectionLimit)//se non si è superato il limite di connessioni
        {
         if(freeConnections.size() > 0)
         {
            con = (Connection)freeConnections.firstElement();
        freeConnections.removeElementAt(0);
            try
            {
             if(con.isClosed())
             {
               con = getConnection();
               NumberConnections--;
               }
            }
            catch(SQLException e)
            { // Se c'è un errore
             con = getConnection();
            }
         }
         else
         { // se la coda delle connessioni libere è vuota
           con = newConnection();
         }
        return con;
      }
      else
      {
       System.out.println("Limite di connessioni raggiunto! riprovare più tardi");
      }
      return null;
      }
        
      // Il metodo newConnection restituisce una nuova connessione
      private Connection newConnection()
      {
        Connection con = null;
        try
        {
           Class.forName(dbDriver);
           con = DriverManager.getConnection(dbUrl);
        }
        catch(Exception e)
        {
          System.out.println("Errore in fase di creazione della connessione");
        }
        NumberConnections++;
        return con;
     }
      
      public synchronized void releaseConnection(Connection con)
        {
            freeConnections.add(con);
        }
    }



    Use:

    p=ConnectionPool.getConnectionPool();
    Conncetion conn=p.getConnection();
    ...
    query and updates...
    ...
    p.releaseConnection(conn);

    ps. don't close connections!!
  4. A connection Pool[ Go to top ]

    This is what I was looking for....

    Driver myDriver = (Driver) Class.forName("weblogic.jdbc.pool.Driver").newInstance();
    conn = DriverManager.getConnection("jdbc:weblogic:pool:testPool",null);

    Thanks for all those stuffs...
    Sabyasachi