Feedback
Did this article resolve your question/issue?

   

Article

Behavior of connection.Isvalid() on a multithreaded connection

Information

 
TitleBehavior of connection.Isvalid() on a multithreaded connection
URL NameBehavior-of-connection-Isvalid-on-a-multithreaded-connection
Article Number000184513
EnvironmentProduct: Connect for JDBC
Version: All supported versions
OS: Java
Database: All supported databases
Application: All supported application
Question/Problem Description
In a multithreaded application, the first thread executes a statement that takes 1 minute.  Second thread executes connection.isvalid(5), which should return in 5 seconds. Instead, the isValid() call waits for the statement on the other thread to return.
 
Steps to Reproduce
Clarifying Information
From the JDBC Specification:

isValid
boolean isValid(int timeout)
                throws SQLException


Returns true if the connection has not been closed and is still valid. 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.

The query submitted by the driver to validate the connection shall be executed in the context of the current transaction.

Parameters:
timeout - - The time in seconds to wait for the database operation used to validate the connection to complete. If the timeout period expires before the operation completes, this method returns false. A value of 0 indicates a timeout is not applied to the database operation.

Returns:
true if the connection is valid, false otherwise


https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#isValid(int)
Error Message
Defect Number
Enhancement Number
Cause
This is expected behavior.  Since the driver needs to submit a query to the server in order to determine whether the connection is valid or not, if the connection is busy executing or processing results for a statement, then isValid is not going to gain access to the connection until all the processing of that statement is complete. Once the connection becomes available for isValid to execute its database operation, then the timer starts for the timeout value as is described by the specification.
Resolution
When isValid() is called, the driver submits a query on the connection to verify that the connection is still valid. If a connection is in use, then the driver's isValid method will wait for the connection to become available before proceeding to assess the connection's state. 
 
Workaround
Notes
Last Modified Date7/13/2016 8:51 AM
Files
Disclaimer The origins of the information on this site may be internal or external to Progress Software Corporation (“Progress”). Progress Software Corporation makes all reasonable efforts to verify this information. However, the information provided is for your information only. Progress Software Corporation makes no explicit or implied claims to the validity of this information.

Any sample code provided on this site is not supported under any Progress support program or service. The sample code is provided on an "AS IS" basis. Progress makes no warranties, express or implied, and disclaims all implied warranties including, without limitation, the implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample code is borne by the user. In no event shall Progress, its employees, or anyone else involved in the creation, production, or delivery of the code be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample code, even if Progress has been advised of the possibility of such damages.