我正在開發(fā)一個(gè) Java 應(yīng)用程序,該應(yīng)用程序可以連接到我的辦公室網(wǎng)絡(luò)或不連接。我的問題是,當(dāng)用戶未連接時(shí),DriverManager.getConnection()最多需要 20 秒才能最終釋放它無法連接到數(shù)據(jù)庫。將我連接或不連接到服務(wù)器的代碼: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貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊
添加以connectionString
毫秒為單位的連接超時(shí):
&connectTimeout=5000
連接超時(shí)
套接字連接超時(shí)(以毫秒為單位),0 表示沒有超時(shí)。僅適用于 JDK-1.4 或更新版本。默認(rèn)為“0”。
添加回答
舉報(bào)
0/150
提交
取消