Bug #34703 isValild() aborts Connection on timeout
Submitted: 20 Feb 2008 19:46 Modified: 1 Mar 2008 12:29
Reporter: Marian Grigoras Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.1.5 OS:Windows (WinXP)
Assigned to: CPU Architecture:Any
Tags: isValid

[20 Feb 2008 19:46] Marian Grigoras
Description:
On calling the isValid(int timeout) method on a Connection, if the timeout is not-zero, after the given timeout the Connection is invalidated, even if the Connection was valid.

How to repeat:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class BugIsValid
{
	public static void main(String args[])
	{
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection("jdbc:mysql:///yourDB", "userName", "userPass");
			
			// Connection should be valid, with a good configuration before
			System.out.println("isValid = " + con.isValid(5));
			
			// After 5 seconds, the timeout reacts as if the Connection were not valid
			System.out.print("Starting 6 second timer ... ");
			Thread.sleep(6*1000);
			System.out.println("done.");

			// Connection should still be valid, but is not!
			System.out.println("isValid = " + con.isValid(0));

		} catch(Exception e) {
			System.err.println("Exception: " + e.getMessage());
		} finally {
			try {
				if(con != null)
					con.close();
			} catch(SQLException e) {}
		}
	}
}

Suggested fix:
Workaround:
If it's ok for your application, set the timeout to 0(zero) - the timeout TimerTask never gets started in this case.

Suggested fix:
The created TimerTask() is annonymous, but it should be assigned to the variable timeoutTask.

Later on, if the Connection is valid, the timeoutTask is correctly reseted.
The problem is that the timeoutTask never gets assigned the value of the scheduled TimerTask.
[27 Feb 2008 5:24] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/43031
[27 Feb 2008 17:12] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/43086
[1 Mar 2008 12:29] MC Brown
A note has been added to the 5.1.6 changelog: 

When calling isValid() on an active connection, if the timeout is non-zero then the Connection is invalidated even if the Connection is valid.