Java套接字超时问题导致打开的文件过多

时间:2015-10-28 15:58:11

标签: java sockets socket.io

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

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

  }
} 

1 个答案:

答案 0 :(得分:0)

它与套接字超时无关。

您正在泄露数据库连接。您正在从池中获取连接并且永远不会返回它。

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