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.
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.
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.
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.
The Connection class establishes a current database session that you can use to execute SQL statements and return results.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With