我知道其他人已經發布了關於這個,但他們似乎都沒有回答我的問題。每當我的一臺服務器啓動時,它必須連接到數據庫,並且我每天都在4-10臺服務器之間運行,其中一些服務器每隔一段時間就會啓動/關閉。只有有時mysql會拒絕localhost連接。Java JDBC連接被拒絕
連接MySQL代碼:
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/DATABASE?connectTimeout=15000", "USERNAME", "PASSWORD");
凡說DATABASE,USERNAME &密碼,那些只是隱藏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
線17 me.Swedz.core.MySQL.createPool是其中連接被初始化(在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();
的可能的複製[com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信鏈路故障(http://stackoverflow.com/questions/2983248/com-mysql-jdbc -exceptions-jdbc4-communicationsexception-communications-link-fai) – zombie
不,@zombie。這不是重複的。如果你仔細閱讀這篇文章,你會發現我的問題只發生在有時。 「重複」職位始終發生,只有我有時。 – Swedz
如果它是一個剛剛啓動的服務器,同時運行數據庫和應用程序,那麼在您的應用程序嘗試連接之前,mysql是否還沒有運行?如果是這種情況,只需等待一秒或兩秒鐘,然後再次嘗試連接,即可處理此異常。 – Rob