我的连接到MySQL数据库的程序运行良好。然后,在没有更改任何用于建立连接的代码的情况下,我得到了这个异常:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.发生了什么?
用于获取连接的代码:
private static Connection getDBConnection() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
String username = "user";
String password = "pass";
String url = "jdbc:mysql://www.domain.com:3306/dbName?connectTimeout=3000";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}发布于 2010-01-23 11:25:20
这是一个封装的异常,并不是很有趣。它是异常的根本原因,它实际上告诉了我们一些关于根本原因的信息。请在堆栈跟踪中进一步查看。然后你面对SQLException: Connection refused或SQLException: Connection timed out的可能性很大。
如果在您的情况下也是如此,那么所有可能的原因都是:
本地
要解决其中之一或两者之一,请遵循以下建议:
ping.--skip-networking DB的--skip-networking--skip-networking DNS防火墙和/或配置防火墙/代理的情况下启动了mysqld,以允许/转发端口。< my.cnf >G234
顺便说一句(并且与实际问题无关),您不必在每次getConnection()调用时都加载JDBC驱动程序。在启动过程中只需要一次就足够了。
发布于 2012-04-27 23:28:59
检查DB服务器上的等待超时设置。有时它默认为10秒。这将在10秒内断开连接。
mysql> show global variables like '%time%' ;更新它,使其类似于28800
mysql> SET GLOBAL wait_timeout = 28800;发布于 2015-10-16 12:24:19
我得到了同样的错误,但后来我找出了它,因为的Mysql服务器当时没有运行。
因此,要更改服务器的状态
然后
然后,您将在状态列下看到它未运行
右键单击
希望这能有所帮助。
https://stackoverflow.com/questions/2121829
复制相似问题