Reputation: 6187
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?
Upvotes: 9
Views: 44342
Reputation: 2086
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 |
+--------------------+
Upvotes: 8
Reputation: 43
Adding this before you call getConnection.
...
DriverManager.setLoginTimeout(2);
DriverManager.getConnection(connectString);
...
Worked in my case.
Upvotes: 4
Reputation: 9989
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 .
Upvotes: 18
Reputation: 20755
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
Upvotes: 1
Reputation: 8657
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;
Upvotes: -5