Sometimes my MySQL connecting disconnects after 8 hours (standard wait_timeout value). And everytime I get the following error:
[Server thread/WARN]: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 432,896,866 milliseconds ago. The last packet sent successfully to the server was 432,897,054 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
It says that i have to set autoReconnect to true, but this is how I connect to MySQL:
final Properties prop=new Properties();
prop.setProperty("user",user);
prop.setProperty("password",password);
prop.setProperty("useSSL","true");
prop.setProperty("autoReconnect","true");
con=DriverManager.getConnection("jdbc:mysql://"+host+"/"+database,prop);
As you can see: autoReconnect is enabled, so why is my connection disconnecting and how can i fix that?
Related
This question already has answers here:
java.net.UnknownHostException: Invalid hostname for server: local
(14 answers)
Kafka Consumer Error - xxxx nodename nor servname provided, or not known
(1 answer)
Closed 1 year ago.
I've seen many topics with that issue but unfortunately I couldn't solve my problem. I've installed MySQL on my server (CentOS 7), I'm able to connect with my DB via shell or MySQL Workbench but when I'm trying to run my Java app, then I'm getting errors like these:
com.mysql.cj.jdbc.exceptions.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.
Caused by: java.net.UnknownHostException: (my server's ip): nodename nor servname provided, or not known
Firewall or proxy can't block connection since I can connect with DB via Workbench or shell..
What's more, I've followed this topic: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure and:
ip address in application.properties is correct
not sure why it could be not recognized
port number is configured to 3306
my DB is running
server accept tcp/ip connections
can't run out of connections since I can connect in same time with Workbench and shell
firewall and proxy is configured to don't block these connections
any ideas what can be wrong with that?
EDIT:
My application.properties configuartion
#Database - deployment
spring.datasource.url=jdbc:mysql://xxx.xx.xxx.xxx/mydbname?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Warsaw
spring.datasource.username=mylogin
spring.datasource.password=mypassword
#Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=create
spring.jpa.database=mysql
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
For my Springboot project I can't connect to a mysql that I had hosted on a site. With a local mysql (workbench) it works well but when I replace the localhost url with that of the host it does not work and I have the following error message:
com.mysql.cj.jdbc.exceptions.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.
here is my code in application properties:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/word?
spring.datasource.username=root
spring.datasource.password=root
I replace the following line of code with my Database Host:
spring.datasource.url=jdbc:mysql://db.example.net:3306/word?
spring.datasource.url=jdbc:mysql://db.example.net:3306/word?
It could be many things
IPTables/firewalld based firewall on either machines
Firewall on the network
Or simply IP is not reachable
If all the above 3 is done right then it could be a config on MySQL server side to not accept remote connections. Flip that flag (and possibly restart the MySQL service)
I would start from pinging the DB IP. If that's accessible you can rule out the line number 3. Then next I would look for the aforementioned config on mysql server side.
I just received a message from the support, they don't allow connection from any IP address. Thanks for answers!
I'm trying to add a server as a failover for SQL Server and it is not using the port 1443, I'm using the port 2776. I'm trying to specify it but everything I tried didn't work. How could I do that?
private String url = "jdbc:sqlserver://server1:2776;DatabaseName=db;failoverPartner=server2";
I've tried the following configs, but none of them worked.
...failoverPartner=server2:2776
...failoverPartner=server2,2776
...failoverPartner=server2\\db
But everytime I get exception.
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host server2, port 1433 has failed. Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host server2:2776, port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host server2, 2776, port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
From the documentation Connecting to SQL Server with the JDBC Driver - Setting the Connection Properties documentation, property failoverPartner:
Note: The driver does not support specifying the server instance port number for the failover partner instance as part of the failoverPartner property in the connection string. However, specifying the serverName, instanceName and portNumber properties of the principal server instance and failoverPartner property of the failover partner instance in the same connection string is supported.
Worded very confusingly, but it looks like you can't specify a port number for the failover server.
From KB-2284190 From Microsoft (Applications cannot connect to a mirror partner server when using port number in the failoverPartner attribute of the connection string ) I'm pretty much reading the same:
CAUSE
Both the error messages occur due to the fact that SQL Server JDBC drivers (all versions) do not support parsing of port number for FailoverPartner connection string attribute and rely on DNS and SQL Server Browser service (for named instances only) to resolve the connection information for the partner server. In environments wherein the conditions discussed in the Symptoms sections are met, the JDBC driver is not able to resolve the partner server information and hence you get the error message discussed above.
This behavior of SQL mirroring infrastructure is by design. For this reason, Microsoft JDBC Driver version 3.0 supports failover partner attribute value only in the format <server_name>[\<SQL_Server_instance_name>].
RESOLUTION
To work around this problem, use one of the following methods in environments where database mirroring is involved:
For default instances of SQL server that are part of mirroring configuration ensure that they are listening on the default port 1433 for TCP connections.
For named instances, ensure that SQL browser service is running and port 1434 is not blocked on the network and server B is not configured as a hidden instance.
In conclusion, do one of the following:
Specify the failover server to listen on port 1433 for TCP.
Make a named instance and specify the failoverPartner as server_name\instance_name. Make sure SQL Server Browser service is running and port 1434 is not blocked .
I trying to get connection.
I have 3 different variants:
1:
Connection connection =
DriverManager.getConnection("jdbc:mysql://127.0.0.1/db1", "root", "");
2:
Connection connection =
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db1",
"root", "");
3.
Connection connection =
DriverManager.getConnection("jdbc:mysql://127.0.0.1:7777/db1",
"root", "");
Using each of these connections I can work with database properly.
But about using this parameter you can find mentions in almost every jdbc tutorial.
Expected result - get exception at least in third variant because this port obviously wrong.
Why do not I see error message?
port is not ignored (I've connected to a MySQL server running on a non-default port using this syntax).
The first two options are identical, since 3306 is the default port for MySQL. Not sure why the 3rd option works. Perhaps if you supply an invalid port, it falls back to the default port.
MySQL connection URL has the following format:
jdbc:mysql://[host][:port]/[database]
host - The host name where MySQL server is running.
Default is 127.0.0.1 - the IP address of localhost.
port - The port number where MySQL is listening for connection.
Default is 3306.
Database - The name of an existing database on MySQL server.
If not specified, the connection starts no current database.
In the first case it works with default port 3306. Then it is same as the second case.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet success fully received from the server was 176,705,355 milliseconds ago. The last packet sent successfully to the server was 176,705,366 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
I am getting above exception when ever insert query in log4j.xml gets executed??