jdbc mysql loginTimeout不能正常工作

有人可以解释为什么以下程序在3秒后超时,我在12秒后设置它。 我故意关闭mysql服务器来测试mysql服务器无法访问的情况。

import java.sql.Connection; import java.sql.DriverManager; /** * * @author dhd */ public class TestMysql { static Thread trd; public static void main(String[] argv) { keepTrack(); try { DriverManager.setLoginTimeout(12); Class.forName("com.mysql.jdbc.Driver"); Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/driving", "root", ""); } catch (Exception ex) { System.err.println(ex.getMessage()); trd.stop(); } } public static void keepTrack() { trd = new Thread(new Runnable() { @Override public void run() { int i = 1; while (true) { System.out.println(i); try { Thread.sleep(1000); } catch (Exception ex) { } i++; } } }); trd.start(); } } 

输出是:

跑:
 1
 2
 3
通信链路故障

成功发送到服务器的最后一个数据包是0毫秒前。 驱动程序未收到来自服务器的任何数据包。
建立成功(总时间:3秒)。

从netbeans运行。 在问我为什么需要这个之前,请先回答。 谢谢

如果MySQL服务器未运行,则连接不会超时; 操作系统立即回复“连接被拒绝”错误。 要使连接超时,您可以做的是配置防火墙以丢弃所有到达MySQL端口的数据包。