sachinpkale
sachinpkale

Reputation: 999

MySql isValid method for timeout

Can I use the isValid(0) function to check whether the connection is still active (alive)?

I am getting following exception :

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

The last packet successfully received from the server was milliseconds ago. The last packet sent successfully to the server was milliseconds ago, which is longer than the server configured value of 'wait_timeout'.

You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem

I can't increase the 'wait_timeout' value.

Also I tried with autoReconnect=true but I need to handle the exception anyway.

I got to know about the isValid() function. But I couldn't understand what kind of validity it checks.

Would using isValid(0) help me solve timeout problem?

Upvotes: 4

Views: 1742

Answers (1)

Alain Collins
Alain Collins

Reputation: 16362

According to the doc, isValid is required to run "a query", so it should be useful.

The driver shall submit a query on the connection or use some other mechanism that positively verifies the connection is still valid when this method is called.

Seems like something your connection pool should be doing for you...

Upvotes: 1

Related Questions