我正在开发一个 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
提交
取消
