【问题标题】:Java socket timeout issue causing too many open filesJava 套接字超时问题导致打开的文件过多
【发布时间】:2015-10-28 15:58:11
【问题描述】:

我有一个运行了一段时间的 java 套接字。今天我注意到建立了很多套接字连接,但是几个小时后我得到了太多打开的文件。我已经设置了 timeout 但不知何故它没有超时我不知道为什么。下面是我的代码的 sn-p。在我的代码中,我在日志中注意到连接已建立并在此行停止-

System.out.println("\n\n Trying establish a new db connection ");
dbconn1 = connectionPool.getConnection();  

可能它没有从池中获取连接,但没有错误或任何套接字连接超时。有什么解决办法吗?

      private Socket receivedSocketConn1;
ConnectionHandler(Socket receivedSocketConn1) {
  this.receivedSocketConn1=receivedSocketConn1;
}
BufferedWriter writer1 = null; 
 Connection dbconn1 = null;     
     public void run() { // etc
     writer1 = null;
     String message="";
     BufferedReader reader1 = null; 
     try { 

         writer1 =  new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream()));
         reader1 = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream()));
         receivedSocketConn1.setSoTimeout(60000);
         int nextChar=0;
         while ((nextChar=reader1.read()) != -1) {    
             message += (char) nextChar; 
             if (nextChar == '*'){

                 try{   
                    System.out.println("\n\n Trying establish a new db connection ");
                    dbconn1 = connectionPool.getConnection();
                    dbconn1.setAutoCommit(false);
                    System.out.println("\n\n Checking db connection status "+dbconn1.isClosed());
                    if ((dbconn1 == null) || dbconn1.isClosed()) {
                         System.out.println("\n\n db connection status is closed");
                         dbconn1 = connectionPool.getConnection();
                         dbconn1.setAutoCommit(false);
//other codes follow here.
                    }
                 }
                 catch (SQLException ex){ 
                   System.out.println("Error SQL Exception : "+ex.toString());
                   ex.printStackTrace(System.out);
                   try{    
                     dbconn1.rollback();  
                   } 
                    catch (Exception rollback) {    
                     System.out.println("\nRollback dbconn1 :");  
                     rollback.printStackTrace(System.out);
                   }
                }
                catch (Exception e){
                   System.out.println("\nSQL Error here :");
                   e.printStackTrace(System.out);
                  try{    
                   dbconn1.rollback();  
                  } 
                  catch (Exception rollback) {    
                    System.out.println("\nRollback dbconn1 :");  
                    rollback.printStackTrace(System.out);
                  }
                }
                finally{
                   try {

                   if ( dbconn1 != null ) {
                     dbconn1.close();
                     System.out.println("\n\n dbConn1 is being closed");
                  }

                  }
                 catch(SQLException ex){
                   System.out.println("SQLException has been caught for dbConn1 close");
                   ex.printStackTrace();
               }               
             } 

           }
       }


      }
      catch (SocketTimeoutException ex){ 
           System.out.println("SocketTimeoutException has been caught ");
           ex.printStackTrace();
      }  
      catch (IOException ex) { 
           System.out.println("IOException has been caught ");
           ex.printStackTrace();
      }  
      catch (Exception ex) { 

           System.out.println("Exception has been caught");
           ex.printStackTrace(System.out);
      }      
      finally{

        try {
            if (writer1 != null ) {
                writer1.close();
            }               
        }
        catch(IOException ex){
           System.out.println("IOException has been caught for finally");
           ex.printStackTrace(System.out);
        }
      }
}



public static void main(String[] args) {
try {
  // setup the connection pool
        BoneCPConfig config = new BoneCPConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/****"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
        config.setUsername("***"); 
        config.setPassword("***");      
        config.setMinConnectionsPerPartition(5);
        config.setMaxConnectionsPerPartition(40);
        config.setPartitionCount(1);
        connectionPool = new BoneCP(config); // setup the connection pool           
  }
  catch (SQLException e) {
        e.printStackTrace(System.out);
  }
  try 
  {
           final ServerSocket serverSocketConn = new ServerSocket(8888);                
           while (true) 
                {
                    try 
                    {
                            Socket socketConn1 = serverSocketConn.accept();
                            new Thread(new ConnectionHandler(socketConn1)).start();                     
                    }
                    catch(Exception e)
                    {
                        System.out.println("Socket Accepting has been caught);
                        e.printStackTrace(System.out);
                    }
                }
  } 
  catch (Exception e) 
  {
     System.out.println("Socket Conn has been caught );
     e.printStackTrace(System.out);

  }
} 

【问题讨论】:

  • 尝试关闭BufferedReader/Writer到
  • 是的,我已经尝试过我的问题是为什么没有触发超时?
  • 你使用什么样的 Socket 你在哪里使用 Socket 我在你的代码中找不到它
  • 以这种方式使用套接字和数据库不是一个好主意,它非常容易出错并且难以维护。使用一些框架或 Rest 模板或其他东西。
  • 嗨,我已经添加了创建数据库池的主要功能,并展示了我如何接受连接并在那里处理它们。

标签: java sockets socket.io


【解决方案1】:

与socket超时无关。

您正在泄漏 数据库 连接。您正在从池中获得一个连接并且永远不会返回它。

NB 任何返回 null 或关闭连接的连接池都有严重错误。您不需要该测试或以下代码。

【讨论】:

  • 对不起,我已经更新了代码 sn-p,在我的代码中,当我创建每个新连接并最终阻止我关闭它时,我尝试并捕获它。我添加了之前没有包含的代码以保持代码简短。
  • 所以我很确定每次最后都会关闭连接。
  • 那么你期待什么?我只能评论您发布的代码。我的 NB 仍然正确。
  • 抱歉缺少部分。由于我正在以适当的方式关闭数据库连接。还有什么可能导致打开文件过多的问题?
  • 但是如果池用尽了,应该会捕获或生成一些错误消息,但我从来没有看到任何我不断连接套接字并暂停 System.out.println("\n\n尝试建立一个新的数据库连接");我在我的问题中提到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 2019-10-21
  • 2010-10-27
  • 1970-01-01
  • 2012-03-25
  • 1970-01-01
相关资源
最近更新 更多