Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 541
Ian Avatar asked Jun 22 '16 15:06

Ian


People also ask

What does connection prepareStatement do?

prepareStatement. Creates a PreparedStatement object for sending parameterized SQL statements to the database. A SQL statement with or without IN parameters can be pre-compiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.

How do I know if my JDBC connection is successful?

Since version 1.6, the Connection class provides a validation method. First, it submits a validation query to the database. Second, it uses the timeout parameter as a threshold for the operation. Finally, the connection is marked as valid if the operation succeeds within the timeout.

What happens when connection is not closed?

If you don't close it, it leaks, and ties up server resources. @EJP The connection itself might be thread-safe (required by JDBC), but the applications use of the connection is probably not threadsafe. Think of things like different transaction isolation, boundaries (commit/rollback/autocommit) etc.

What does Connection class do?

The Connection class establishes a current database session that you can use to execute SQL statements and return results.


2 Answers

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) {
            var1.close();
        }

    }

    return var3;
}

I believe that other vendors do something similar.

like image 174
Grisha Weintraub Avatar answered Sep 29 '22 11:09

Grisha Weintraub


In case of the Oracle JDBC thin driver, what isValid() does depends on the version of the driver and the Database. Starting in 11g, the JDBC thin driver uses OPING which is a very lightweight RPC (the smallest possible roundtrip). Prior to 11gR2 the driver was executing a SELECT query which is more expensive. Also of the database is pre-10g, then OPING isn't supported and the driver executes a SELECT query.

like image 40
Jean de Lavarene Avatar answered Sep 29 '22 11:09

Jean de Lavarene