【问题标题】:Java connection pooling optionsJava 连接池选项
【发布时间】:2012-04-02 11:49:00
【问题描述】:

我们计划在我们的 java 应用程序中实现连接池。我们用谷歌搜索了很多,例如 BoneCp、DbPool、Apache、c3p0、DbCp 等。我们现在发现的问题是很难决定应用哪一个,因为有些已经过时了。哪种方法是最好的解决方案?

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

    }

}

【问题讨论】:

标签: java connection-pooling


【解决方案1】:

首先从连接池中确定您需要什么,然后寻找提供该功能的库。

现在选择一个,使用已经在网上找到的流行观点或通过在 SO 上在这里提出具体问题。

接下来,再次根据您对池的需求,为连接池功能创建一个抽象层,并使用所选库实现。

这样,如果您对底层库不满意,即使在开发过程中也可以更改它。

【讨论】:

  • 我已经添加了我的代码的 sn-p。它基于每个套接字连接的套接字连接,我将打开一个连接,当有数据继续处理它并最终关闭连接。所以我认为 createConnection 是我应该调用连接池的地方吗?
猜你喜欢
  • 2011-09-30
  • 2013-04-08
  • 2012-05-18
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 2013-01-23
相关资源
最近更新 更多