Ian Ian - 11 months ago 44
Java Question

What does Connection.isValid(time) actually do to check if connection if valid?

Using OJDBC process does the isValid method use to check if the the connection is still alive? I'm trying to figure out what impact it could have on the database and how heavy this process is. e.g. does it request a column, or just ping the db with a couple bytes of data.


Each vendor implements jdbc methods differently. For example Oracle's implementation is :

public boolean isValid(int var1) throws SQLException {
    return this.pingDatabase(var1) == 0;

And pingDatabase simply executes select x from dual :

int doPingDatabase() throws SQLException {
    Statement var1 = null;

    byte var3;
    try {
        var1 = this.createStatement();
        ((oracle.jdbc.OracleStatement)var1).defineColumnType(1, 12, 1);
        var1.executeQuery("SELECT \'x\' FROM DUAL");
        return 0;
    } catch (SQLException var7) {
        var3 = -1;
    } finally {
        if(var1 != null) {


    return var3;

I believe that other vendors do something similar.