[CONJ-120] Connection.isValid(int) does not work when connection is closed Created: 2014-10-30  Updated: 2014-11-26  Resolved: 2014-11-26

Status: Closed
Project: MariaDB Connector/J
Component/s: None
Affects Version/s: 1.1.7
Fix Version/s: 1.1.8

Type: Bug Priority: Minor
Reporter: Lennart Schedin Assignee: Massimo Siani (Inactive)
Resolution: Fixed Votes: 0
Labels: None

Attachments: Java Source File ConnectionIsValidTest.java    

 Description   

Background:
The method Connection.isValid(int) method http://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#isValid%28int%29 has this documentation for Returns:
“true if the connection is valid, false otherwise”
And this documentation for Throws:
“SQLException - if the value supplied for timeout is less then 0”

How to reproduce:

  1. Download and run the attached JUnit test file ConnectionIsValidTest.java
  2. Run the test with MariaDB JDBC
  3. Run the test with Mysql JDBC (see instructions in the file for details)
  4. Run the test with H2 in memory database

Psedo-code:

  1. Create a connection to the database server
  2. Kill the connection in some way (for example with KILL CONNECTION)
  3. Check the status of the connection with Connection.isValid(10).

Actual MariaDB JDBC:
isValid_shouldThrowExceptionWithNegativeTimeout: fails because it does not throw an SQLException
isValid_testWorkingConnection: pass
isValid_closedConnection: fails with a NullPointerException
isValid_connectionThatTimesOutByServer: fails with SQLException
isValid_connectionThatIsKilledExternally: fails with SQLException

Actual Mysql JDBC:
isValid_shouldThrowExceptionWithNegativeTimeout: fails because it does not throw an SQLException
isValid_testWorkingConnection: pass
isValid_closedConnection: pass
isValid_connectionThatTimesOutByServer: pass
isValid_connectionThatIsKilledExternally: pass

Actual H2:
isValid_shouldThrowExceptionWithNegativeTimeout: fails because it does not throw an SQLException
isValid_testWorkingConnection: pass
isValid_closedConnection: pass
isValid_connectionThatTimesOutByServer: ignored
isValid_connectionThatIsKilledExternally: ignored

Expected:
Every test case should pass (except the ignored test cases in the H2 case).

However every JDBC implementation has ignored that a negative timeout should throw an SQLException. This is probably such a minor detail that it is not worth fixing.



 Comments   
Comment by Massimo Siani (Inactive) [ 2014-10-30 ]

New branch https://code.launchpad.net/~massimo-siani/mariadb-java-client/CONJ-120

Comment by Lennart Schedin [ 2014-11-04 ]

I checked the code in the branch briefly and I think that could work. However I'm not sure what kind of Exception that should trigger the isValid() method to throw an exception? Should it ever throw an Exception (except the negative time)?

Comment by Massimo Siani (Inactive) [ 2014-11-04 ]

isValid only throws a SQLException when the provided timeout is negative. It has to return false in the case the connection has been closed, doesn't it?

Comment by Lennart Schedin [ 2014-11-04 ]

You might be correct. I was unsure in what situations the code might reach the row with SQLExceptionMapper.throwException(e, this, null) and throw an exception. Should the call to SQLExceptionMapper.throwException() even be there?

        } catch (QueryException e) {
        	// if timed out, return false
        	if (e.getSqlState() == SQLExceptionMapper.SQLStates.CONNECTION_EXCEPTION.getSqlState()) {
        		return false;
        	}
            SQLExceptionMapper.throwException(e, this, null);
            return false;
        }

Generated at Thu Feb 08 03:13:19 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.