Swedz Swedz - 5 months ago 20
Java Question

Java JDBC connection refused

I am aware other people have posted about this, but none of them seemed to have answered my issue. Whenever one of my servers boot up, it has to connect to the database, and I'm running from anywhere between 4-10 servers all day, and some of them booting up/down every so often. And only SOMETIMES the mysql refuses the localhost connection.

MySQL connection code:

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


Where it says DATABASE, USERNAME & PASSWORD, those are just to hide the MySQL user information.

I get this error only SOMETIMES when connecting:

[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


Line 17 in me.Swedz.core.MySQL.createPool is where the connection is being initialized (the second line in the MySQL connection code provided before this error).

My bind-address is currently = 127.0.0.1, which works best for me. Because when it's set to 0.0.0.0 or localhost, this error occurs more frequently.

This is continuing to happen more and more frequently over time. It's really getting frustrating and near to impossible to do work on my servers with this issue.

Edit: I believe I have managed to solve the issue on my own. I have changed my connection code to something a little different. And it seems to not error out at all! My new MySQL connection code:

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

Answer

I have managed to solve the issue on my own. I have changed my connection code to something a little different. And it seems to not error out at all! My new MySQL connection code:

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