tags:

views:

349

answers:

1

Hi,

I have trying to check whether connection is valid or not and using isValid() method of java.sql.Connection. But this method doesn't return and hangs.

Is there anything I am missing or this method requires any configuration? Using mysql-connector-java-5.0.7-bin.jar

Thanks, Shahid

A: 

I don't know why but in 5.0.x versions this method is abstract. It's implemented correctly in 5.1.x.

Using mysql-connector-java-5.1.1-bin.jar:

try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection (
                                  "jdbc:mysql://localhost/","root", "root");
    System.out.println("Is valid? " + connection.isValid(10));
    connection.close();
    System.out.println("Is valid? " + connection.isValid(10));
} catch (Exception e) {
    e.printStackTrace();
}

Returns:

Is valid? true
Is valid? false
Fede