How to set a connection timeout on the MySQL JDBC driver?

mysql connection timeout
jdbc connection timeout exception
mysql autoreconnect
jdbctemplate set connection timeout
debug mysql connection timeout
sybase jdbc connection timeout
jdbi connection timeout
useserverprepstmts

I'm seeing the JDBC MySQL driver consistently fail connection attempts to a stopped MySQL after 10 seconds, but I'd like to change that timeout.

I tried adding ?connectTimeout=2000&socketTimeout=2000 to the connection URI, but that didn't make a difference.

Is there a way to customize how long it takes for the Driver to return a timeout while connecting to MySQL?

Thanks!

Eduard

You can change the default value in MySQL configuration file (connect-timeout option in mysqld section) -

[mysqld]
connect-timeout=100

If this file is not accessible for you, then you can set this value using this statement -

SET GLOBAL connect_timeout=100;

MySQL :: MySQL Connector/J 5.1 Developer Guide :: 5.3 , Configuration properties define how Connector/J will make a connection to a MySQL Timeout for socket connect (in milliseconds), with 0 being no timeout. If connecting using TCP/IP, should the driver set SO_RCV_BUF to the given value? In this tutorial, you will learn how to connect to MySQL database using JDBC Connection object. To connect to MySQL database from a Java program, you need to do the following steps: Load the MySQL Connector/J into your program. Create a new Connection object from the DriverManager class. Then you can use this Connection object to execute queries.

I tried adding ?connectTimeout=2000&socketTimeout=2000 to the connection URI, but that didn't make a difference.

This is exactly how it's configured, eg

jdbc:mysql://aaa.bbb.ccc.rds.amazonaws.com:1234/hello?connectTimeout=5000&socketTimeout=30000

See https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html .

How to Set JDBC Connection Timeout in Java?, Connection Timeout with MySQL Database MySQL has its wait_timeout variable default value set to 28800 seconds (8 hours). into the JDBC URL in stash-config.properties file to use MySQL interactive_timeout default  Stash is shipped with a connection test interval default of 10 minutes. MySQL has its wait_timeout variable default value set to 28800 seconds (8 hours). Therefore, if both sides of the connection still keep the defaults, the problem will never happen, as MySQL will never timeout a connection before Stash does it.

Adding this before you call getConnection.

...
DriverManager.setLoginTimeout(2);
DriverManager.getConnection(connectString);
...

Worked in my case.

Connection Timeout with MySQL Database, MySQL JDBC Driver and URL connection information for connecting to MySQL socketTimeout (Timeout on network socket operations (0, the default means no  To run it with java command, we need to load the MySQL JDBC driver manually. Assume everything is stored in the c:\test folder, run it again with this -cp option. > java -cp "c:\test\mysql-connector-java-8.0.16.jar;c:\test" JDBCExample Connected to the database! 3. Maven Project. 3.1 The MySQL JDBC driver is available in the Maven central

You can set the connection timeout using this:

con.query('SET GLOBAL connect_timeout=2000')
con.query('SET GLOBAL wait_timeout=2000')
con.query('SET GLOBAL interactive_timeout=2000')

For more help see MySqlConnection

MySQL JDBC Driver and URL Information, of the version number change) is a Type 4 pure Java JDBC 4.2 driver for the Java 8 Timeout for socket connect (in milliseconds), with 0 being no timeout. You can make this request to show your timeout in MySql. SHOW VARIABLES LIKE 'connect_timeout'; Next you can update it to 60 secondes for exemple. SET GLOBAL connect_timeout=60; If the problem is the Query you can use Statement.setQueryTimeout

[PDF] MySQL Connector/J 8.0 Developer Guide, connectTimeout in MySQL JDBC driver. Timeout for socket connect (in milliseconds), with 0  Configuration properties define how Connector/J will make a connection to a MySQL server. Unless otherwise noted, properties can be set for a DataSource object or for a Connection object. As a JDBC URL parameter in the URL given to java.sql.DriverManager.getConnection () , java.sql.Driver.connect () or the MySQL implementations of the javax

Database timeouts, You can configure your application server to avoid MySQL timeout issues. type​="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" . I am trying to connect to DB using the standard JDBC way. connection = DriverManager.getConnection(url, username, password); Is there a maximum value of timeout on the connection, how long does a connection live, can I increase the value. I want in cases the connection to be open forever , is it a good idea.

Handling MySQL stale connections, Use the default jdbc Connection pool HikariCP --> <dependency> mysql spring​.datasource.url=jdbc:mysql://localhost:3306/springboot2 What if the connection timeout time and the maximum number of connections in the  dB connection time out. The purpose of connection timeout is to keep invalid requests from tying up the server. Unfortunately, sometimes the server can tie up the server, and blame it on the request. This results in valid requests timing out. The most common reason that this occurs is JVM garbage collection.

Comments
  • I am confused....isn't that a mysql daemon setting. What if you want the setting to be client side. ie. my client may not have settings from the daemon server, right? or am I getting this wrong?
  • This is the setting on the MySQL server. It's not going to have any affect on a client trying to connect to that server when it's stopped.