Oracle connectivity in Java
2015-02-14A new small test project is intended to answer the question - what is the proper way to specify network timeout for database connection?
There so many ways in JDBC API:
And every database has it’s own non-standard ways in addition.
But these tests have been lead us to a single conclusion — you must specify network timeouts on driver level. All these JDBC stuff isn’t enough for Oracle database.
This code is suitable for Tomcat JDBC Connection Pool:
// Get datasource some way
Datasource ds = createDataSource(host);
// Set connect (login) timeout
ds.setConnectionProperties(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CONNECT_TIMEOUT + "=3000");
// Set common network read timeout
ds.setConnectionProperties(OracleConnection.CONNECTION_PROPERTY_THIN_READ_TIMEOUT + "=3000");
The proper way to specify these timeouts in JNDI Datasource configuration:
<Resource name="jdbc/oracle"
driverClassName="oracle.jdbc.OracleDriver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
connectionProperties="oracle.net.CONNECT_TIMEOUT=3000;oracle.jdbc.ReadTimeout=3000"
...
/>