-1

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();
Swedz
  • 89
  • 1
  • 2
  • 7
  • Possible duplicate of [com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure](http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai) – Prabhat Jun 28 '16 at 03:13
  • 1
    No, @zombie. This is not a duplicate. If you read the post carefully, you would see that my issue only occurs SOMETIMES. Where the "duplicate" post occurs all the time, and mine only SOMETIMES. – Swedz Jun 28 '16 at 03:19
  • 1
    If it is a freshly-started server running both your DB and app, isn't it possible that mysql is not yet running before your app tries to connect? If this is the case, just handle this exception by waiting a second or two and try connecting again. – Rob Jun 28 '16 at 03:22
  • 1
    Perhaps you've exceed the number of available connections? See "[increase MySQL connections?](http://stackoverflow.com/q/22297773/5221149)" for fix, if so. – Andreas Jun 28 '16 at 03:24
  • You should Open and close the connection when you need it mean at the start and end of the method, prevents lag as well.... – Piyush Gupta Jun 28 '16 at 04:05
  • @Rob, the server is booted a few seconds after the actually machine is booted up. – Swedz Jun 28 '16 at 04:17
  • @Andreas, that seemed to have worked a lot better, it still very rarely occurs now. – Swedz Jun 28 '16 at 04:17
  • @PiyushGupta, I'm aware. I only create one connection when the server boots up and when the server shuts down I close the connection. – Swedz Jun 28 '16 at 04:17
  • @Andreas I have done "show processlist;" and it only shows 3 connections. I'm still occasionally having issues with this. – Swedz Jun 28 '16 at 06:04
  • Are you closing each and every single of your connections after using them? – Aritz Jun 29 '16 at 20:01
  • @XtremeBiker I have already stated this. "I'm aware. I only create one connection when the server boots up and when the server shuts down I close the connection." – Swedz Jun 29 '16 at 21:42
  • You also say you've got 3 connections in show process list.. It's so difficult to guess what's going on with the given info, and specially when this is happening only sometimes. Anyway, are you using this in production stage? Because probably a connection pool library would be easy to set up and would be much more efficient. – Aritz Jun 30 '16 at 04:37
  • @XtremeBiker, I only use 1 database, I don't see a purpose in using a connection pool if I don't use more than 1 database. – Swedz Jul 01 '16 at 02:53
  • @Swedz I think you misunderstand what a connection pool is. It perfectly makes sense with one dB. Have a look at some connection pooling library like Hikari. – Aritz Jul 01 '16 at 06:48
  • @XtremeBiker I'm using eclipse currently, and I've been trying to be able to use HikariCP in my java project. I'm not sure how to do Maven in Eclipse at all... I've attempted it quite a few times, following tutorials and not following tutorials.. It just never seems to work right. I always get a NoClassDefFoundError. – Swedz Jul 01 '16 at 18:59

2 Answers2

0

from the below code,

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

remove connectionTimeout=15000, it lose connection every 15 second may be.

yash
  • 2,101
  • 2
  • 23
  • 32
0

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();
Swedz
  • 89
  • 1
  • 2
  • 7