Question

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?

Was it helpful?

Solution 5

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;

OTHER TIPS

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 .

Adding this before you call getConnection.

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

Worked in my case.

If neither connectTimeout or socketTimeout helps you, you can try adding MAX_EXECUTION_TIME to sessionVariables in the URL:

jdbc:mysql://{host}:{port}/{database}?sessionVariables=MAX_EXECUTION_TIME=123456666

Query to verify:

SELECT @@max_execution_time

Expected output:

+--------------------+ 
|@@max_execution_time| 
+--------------------+ 
| 123456666          | 
+--------------------+

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top