Java JDBC连接被拒绝

时间:2016-06-28 03:06:21

标签: java mysql jdbc

我知道其他人已发布此事,但他们似乎都没有回答我的问题。每当我的一台服务器启动时,它就必须连接到数据库,我整天都在4-10台服务器之间的任何地方运行,其中一些服务器每隔一段时间就启动/启动。并且只有SUSETIMES mysql拒绝localhost连接。

MySQL连接代码:

Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/DATABASE?connectTimeout=15000", "USERNAME", "PASSWORD");

它说DATABASE,USERNAME& PASSWORD,那些只是隐藏MySQL用户信息。

连接时我只收到此错误:

[15:52:13] [Craft Scheduler Thread - 2/WARN]: 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.
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:343)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2334)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2371)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2163)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:378)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.sql.DriverManager.getConnection(DriverManager.java:664)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.sql.DriverManager.getConnection(DriverManager.java:247)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at me.Swedz.core.MySQL.createPool(MySQL.java:17)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at me.Swedz.lobby.Lobby$1.run(Lobby.java:73)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.lang.Thread.run(Thread.java:745)
[15:52:13] [Craft Scheduler Thread - 2/WARN]: Caused by: java.net.ConnectException: Connection refused
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.net.PlainSocketImpl.socketConnect(Native Method)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.net.Socket.connect(Socket.java:589)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at java.lang.reflect.Method.invoke(Method.java:498)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:327)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:292)
[15:52:13] [Craft Scheduler Thread - 2/WARN]:   ... 21 more

me.Swedz.core.MySQL.createPool中的第17行是初始化连接的位置(在此错误之前提供的MySQL连接代码中的第二行)。

我的绑定地址当前= 127.0.0.1,这对我来说效果最好。因为当它设置为0.0.0.0或localhost时,会更频繁地发生此错误。

随着时间的推移,这种情况将继续发生。对于这个问题,我的服务器上的工作真的很令人沮丧,几乎不可能。

编辑:我相信我已经成功解决了这个问题。我已将连接代码更改为略有不同的内容。它似乎没有错误!我的新MySQL连接代码:

MysqlDataSource ds = new MysqlDataSource();
ds.setDatabaseName("DATABASE");
ds.setUser("USERNAME");
ds.setPassword("USER PASSWORD");
ds.setServerName("localhost");
ds.setPort(3306);
connection = ds.getConnection();

2 个答案:

答案 0 :(得分:0)

来自以下代码,

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/DATABASE?connectTimeout=15000", "USERNAME", "PASSWORD");<br/>

删除connectionTimeout=15000,它可能每15秒失去一次连接。

答案 1 :(得分:0)

我已经设法自己解决了这个问题。我已将连接代码更改为略有不同的内容。它似乎没有错误!我的新MySQL连接代码:

MysqlDataSource ds = new MysqlDataSource();
ds.setDatabaseName("DATABASE");
ds.setUser("USERNAME");
ds.setPassword("USER PASSWORD");
ds.setServerName("localhost");
ds.setPort(3306);
connection = ds.getConnection();