2012-03-13 134 views
10

我写了一个Java程序响应和程序连接到数据库我的服务器上,查找记录,写入记录,更新和删除。由于某种原因,查找记录的作品,但大部分的时候,我尝试保存或写它给出了一个错误消息的记录说:无法读取服务器

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 

The last packet successfully received from the server was 9,787 milliseconds ago.  The last packet sent successfully to the server was 8,183 milliseconds ago. 

Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost. 
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2552) 
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3002) 
... 46 more 

任何人都可以解释为什么这是怎么回事? 通常这会给我错误信息,当我尝试添加一个记录后,我运行了超过半分钟的软件。似乎没有连接或什么东西。当我运行该程序,快速编写一个新的记录,它的工作原理

+0

我们展示的代码有意义的部分,这是我们能猜出是唯一的方法发生。 – pcalcao 2012-03-13 15:36:43

+0

您应该仔细管理丢失的连接http://stackoverflow.com/a/8997413/90909 – qrtt1 2012-03-13 15:37:34

+1

我在我的连接器类中有con.close方法。那可能是为什么呢? – Pita 2012-03-13 15:42:09

回答

0

是否遵循/阅读本教程:

Connectivity with MYSQL

你必须为你的异常的一部分,可以对您有用。

我会引用一些关于你的具体的异常,只是尝试:

如果你得到的SQLException:连接被拒绝或连接超时 或MySQL的具体CommunicationsException:通信链路 失败,那么就意味着数据库根本无法访问。这可以 有以下原因之一或更多:JDBC URL

IP地址或主机名是错误的。 JDBC URL中的主机名是本地DNS服务器无法识别的 。端口号在 JDBC URL中缺失或出错。数据库服务器关闭。数据库服务器不接受TCP/IP 连接。数据库服务器已经用完了连接。 Java和DB之间的 中的某些内容阻塞连接,例如防火墙或代理。

为了解决一个或另一个,遵循以下建议:

验证,并用ping测试。请刷新DNS或使用JDBC URL中的IP地址。根据MySQL.cn的my.cnf验证它。启动数据库。 验证mysqld是否在没有--skip-networking选项的情况下启动。 重新启动数据库和修复您的代码因此,它关闭在最后 连接。禁用防火墙和/或配置 防火墙/代理以允许/转发端口。

+0

我跟着它,但我根据你的网址做了正确的事。我认为它的时间会议,这是阻止我做我的分贝。有没有办法阻止超时会话?我已经阅读了关于maxWait的一些不确定的内容,但是无法找到任何关于如何声明该内容的具体内容,以便系统无论等待多长时间才能等待,直到收到响应。 – Pita 2012-03-13 15:58:16

1

有时候这个问题是由于系统RAM的大小造成的。可能你是通过RAM使用缓冲区插入数据。解决这个问题。

  1. 设置自动提交插入数据前禁用。
  2. 数据插入适当的一定量的系统RAM(不是 整)。
  3. 提交查询。
  4. 再次执行步骤2和3直到整个插入不会 完成。

您可以通过下面的代码明白这一点。

public static void main(string args[]) 
{ 
    Connection con = null; 
    Statement stm = null; 
    int i; 
    float ratio; 
    ratio=1.0f; 
    try 
    { 
    Class.forName("com.mysql.jdbc.Driver"); 
    // Connecting to the database 
    con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/demo", 
             "ashish", "impetus"); 
    File f = new File("filler"); // taking the random text data from the file 
           // filler.txt and inserting that string 
           // in filler field of the relations 
    RandomAccessFile r = new RandomAccessFile(f,"r"); 
    Random randomGenerator = new Random(); 
    String strLine=new String(); 
    int r1,flag=0; 
    stm = con.createStatement(); 
    con.setAutoCommit(false) ; 

    int k=0; 
    i=0;     

    long sum2=0; 
    while(k%50000==0) 
    { 
     final long start = currentTimeMillis(); 
     final StringBuilder builder = 
     new StringBuilder("INSERT INTO accounts 
          (aid, bid,abalance,filler) VALUES "); 
     while(i!=naccounts*ratio*scale) 
     { 
     int j=i+1; 
     for(int l=0;l<40;l++) 
     { 
      strLine+=((char)r.read()); 
      r.skipBytes(0); 
     } 
     r1=randomGenerator.nextInt(1500); 
     if(strLine.equals("")) 
     { 
      flag=1; 
     } 
     if(flag!=1) 
     { 
      strLine=strLine.replaceAll("\\s",""); 
      strLine=strLine.replaceAll("\\t",""); 
     }     
     flag=0; 
     if (i%50000!=0) 
     { 
      builder.append(","); 
     } 
     builder.append(format("(%s, %s, %s, '%s')", j, 
         i/naccounts+1, 0, strLine)); 
     strLine=""; 
     r.seek(r1); 
     i++; 
     if(i%50000==0||i>=naccounts*ratio*scale) 
     { 
      final String query = builder.toString(); 
      final PreparedStatement statement1 = con.prepareStatement(query); 
      statement1.execute(); 
      con.commit(); 
      final long stop= currentTimeMillis(); 
      sum2=sum2+(stop-start); 
      statement1.close(); 
     } 
     if(i%50000==0||i>=naccounts*ratio*scale) 
     { 
      break; 
     } 
     } 
     k=k+50000; 
     if(k>naccounts*ratio*scale) 
     { 
     break; 
     } 
    } 
    System.out.println(i+" rows inserted accounts table "); 
    System.out.println("time taken = "+sum2+" milliseconds"); 
    } 
    catch (SQLException e) 
    { 
    System.out.println("Connection Failed! Check output console"); 
    e.printStackTrace(); 
    return; 
    } 
    catch (Exception e) 
    { 
    e.printStackTrace(); 
    } 
} 
4

我有同样的问题。我提到了很多帖子和评论,但是对我而言,工作是改变my.cnf文件的一些参数。希望这将有助于你也....在my.cnf中的[mysqld]部分

设置以下参数

interactive_timeout=180 # "No.of sec. a server waits for activity on interactive connection before closing it" 

wait_timeout=180 # "No. of sec. a server waits for an activity on a connection before closing it" 

max_connect_errors=9999 # "More than this number of interrupted connections from a host this host will be blocked from further connections" 

skip-name-resolve # "Don't resolved host names. All host names are IP's" 
+1

'skip-name-resolve'的值?谢谢 – 2014-12-11 05:52:31