嗨全部 我試圖連接一個數據庫。下面我給出了代碼。我可以連接到數據庫,但如果第一次和第二次之間的差異大於mysql數據庫的「等待超時」,則無法第二次連接。 我正在使用mysql數據庫。我得到「通信鏈路故障」的問題 WAIT_TIMEOUT爲10秒(爲方便起見)爲8小時,我的數據庫 我給你的代碼JDBC:無法連接第二次
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)
「Srver_Path」 - >是一個錯字嗎? – darioo 2010-12-06 08:28:44
你是在代理之後嗎? – 2010-12-06 08:36:05