JDBC:第二次无法连接

时间:2010-12-06 08:26:42

标签: java mysql jdbc

大家好        我正在尝试连接数据库。下面我已经给出了代码。如果第一次和第二次之间的差异大于mysql数据库的“等待超时”,我能够连接到数据库但第二次无法连接。        我正在使用mysql数据库。我收到“通信链路故障”问题        wait_timeout是10秒(为方便起见)我的数据库是8小时        我给你代码

public class DatabaseManager {
 private static Connection jConn;
 private static DatabaseManager manager;

public Boolean getUser(){
  Statement jStmt = null;
  ResultSet rs = null;
  try {

   String query = "SELECT * FROM  USER  ";
    System.out.println(query);
   jStmt = jConn.createStatement();
   rs = jStmt.executeQuery(query);
   while (rs.next()) {

   }
  }catch (Exception e) {
   e.printStackTrace();
   return false;
  }
  return true;
 }

    public static synchronized DatabaseManager getInstance() {
  if (manager == null) {
   try {
    manager = initialize();
   } catch (DatabaseInitializationException e) {
    // e.printStackTrace();
    // throw new RuntimeException("Database Initialization
    // Exception");
   }
  }
  return manager;
 }
        private static DatabaseManager initialize()
   throws DatabaseInitializationException {

  try {
   Class.forName(serverSrv.readPropertyValue("Srver_Path",
     "DBdriver"));
  } catch (ClassNotFoundException e) {
   throw new DatabaseInitializationException(e.getMessage());
  }
  try {
   jConn = DriverManager.getConnection("DB_URL","user","password");
   System.out.println("Excecuted initialize() for Database ");
  } catch (SQLException e) {
   throw new DatabaseInitializationException(e.getMessage());
  }
  return new DatabaseManager();
 }
}

我正在测试

 public static void main(String[] args) {
  long st = System.currentTimeMillis();
  long end ;
  DatabaseManager dm = DatabaseManager.getInstance();

  if( dm.getUser() )
   System.out.println("success");
  else
   System.out.println("failed..");

  do{
   end = System.currentTimeMillis();
  }while( (end-st)<12*1000 );

  if( dm.getUser() )
   System.out.println("success");
  else
   System.out.println("failed..");
 }

它正在提供例外

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

Last packet sent to the server was 0 ms ago.
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 at java.lang.reflect.Constructor.newInstance(Unknown Source)
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
 at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
 at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2873)
 at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2763)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3299)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
 at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2537)
 at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2466)
 at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1383)
 at mypackage.db.DatabaseManager.getUser(DatabaseManager.java:127)
 at mypackage.test.Test.main(Test.java:23)
Caused by: java.net.SocketException: Software caused connection abort: recv failed
 at java.net.SocketInputStream.socketRead0(Native Method)
 at java.net.SocketInputStream.read(Unknown Source)
 at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113)
 at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160)
 at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188)
 at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2329)
 at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2774)

3 个答案:

答案 0 :(得分:1)

也许您应该关闭ResultSet以及Statement。

这个link有一些关于正确执行此操作的提示。

答案 1 :(得分:1)

您的问题是您尝试重复使用 jConn ,超时后会自动关闭。因此,当您再次尝试使用它时会抛出异常。根据你想要做的事情,我会建议(为简单起见)..

  1. 通过键入 jConn.close();
  2. 关闭 jConn
  3. 然后当您需要再次使用db时,使用 jConn = DriverManager.getConnection(“DB_URL”,“user”,“password”)重新连接;

答案 2 :(得分:0)

检查连接超时设置。