9

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Edy Bourne
  • 5,679
  • 13
  • 53
  • 101

5 Answers5

16

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 .

Adrian Baker
  • 9,297
  • 1
  • 26
  • 22
6

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          | 
+--------------------+
Beniamin H
  • 2,048
  • 1
  • 11
  • 17
4

Adding this before you call getConnection.

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

Worked in my case.

1

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

ravibagul91
  • 20,072
  • 5
  • 36
  • 59
-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;
Salah
  • 8,567
  • 3
  • 26
  • 43
  • 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? – Dean Hiller Mar 24 '16 at 12:39
  • 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. – Adrian Baker Mar 08 '17 at 05:48