Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 313
Edy Bourne Avatar asked Jan 25 '14 13:01

Edy Bourne


2 Answers

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 .

like image 181
Adrian Baker Avatar answered Sep 17 '22 19:09

Adrian Baker


Adding this before you call getConnection.

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

Worked in my case.

like image 28
Johan Le Roux Avatar answered Sep 20 '22 19:09

Johan Le Roux