简体   繁体   中英

Java connection pooling options

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);
    }

    }

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM