我正在開發一個 Java 應用程序,該應用程序可以連接到我的辦公室網絡或不連接。我的問題是,當用戶未連接時,DriverManager.getConnection()最多需要 20 秒才能最終釋放它無法連接到數據庫。將我連接或不連接到服務器的代碼:public static Connection getConnection() { try{ //cnx is my Connection object if(cnx ==null || cnx.isClosed()){ Class.forName("com.mysql.jdbc.Driver"); //Get the connection if possible cnx = DriverManager.getConnection(connectionString, user, password); } /* Basically, I'm just checking if "ConfigUser.isConnected()" is true later in the code to know if I should try to execute SQL queries */ ConfigUser.setConnected(true); return cnx; } catch (Exception e){ } ConfigUser.setConnected(false); return null;}我嘗試使用該DriverManager.setLoginTimeout()功能,但它似乎沒有任何改變。
1 回答

德瑪西亞99
TA貢獻1770條經驗 獲得超3個贊
添加以connectionString
毫秒為單位的連接超時:
&connectTimeout=5000
連接超時
套接字連接超時(以毫秒為單位),0 表示沒有超時。僅適用于 JDK-1.4 或更新版本。默認為“0”。
添加回答
舉報
0/150
提交
取消