0

We plan to implement connection pooling into our java application. We google and found a number of it such as BoneCp,DbPool,Apache,c3p0,DbCp and others. The problem now we are finding difficult to make a decision to which one to apply as some are outdated. Which method will be best solution?

public class cServer
{
   class ConnectionHandler implements Runnable {
        ConnectionHandler(Socket receivedSocketConn1) {
          this.receivedSocketConn1=receivedSocketConn1;
        }


       public void run(){
            createConnection();
             while (read the socket values){
               //number of queries to run in terms of select,insert and updates.
             }
            closeConnection();
       }
       void createConnection(){

        try{
        dbconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1?"+"user=user1&password=*******");
        dbconn.setAutoCommit(false);
        }
        catch(Throwable ex){
           ex.printStackTrace(System.out);
        }
     }
    }


    public void main()
    {
    try 
    {
      final ServerSocket serverSocketConn = new ServerSocket(8000);             
      while (true){
        try{
            Socket socketConn1 = serverSocketConn.accept();
            new Thread(new ConnectionHandler(socketConn1)).start();                     
       }
       catch(Exception e){
         e.printStackTrace(System.out);
       }
      }
    } 
    catch (Exception e){
      e.printStackTrace(System.out);
    }

    }

}
user837306
  • 857
  • 3
  • 18
  • 32

1 Answers1

1

First identify what you need from the connection pool and look for the libraries that provide that functionality.

Choose one for now, using popular opinion already found on the net or by asking specific questions here on SO.

Next, again on the basis of what you need from the pool, create an abstraction layer for the connection pooling functionality and implement using the chosen library.

That way you can change the underlying library if you are not happy with it, even during the course of development.

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
  • I have added snippet of my codes. It is based on socket connection for each socket connection I will open a connection and while there is data keep processing it and finally close the connection.So I think the createConnection is where I should call the connection pooling right? – user837306 Mar 17 '12 at 18:52