Oracle JDBC Connection KeepAlive

So, you’ve got a spoiled bloated app or a third party library and it’s fond of manopolizing JDBC connections. There are couple of options with Oracle.

1) Server side option: Oracle Database comes with a Dead Client Detection feature where Oracle Server sends a probe. It can be configured by specifying SQLNET.EXPIRE_TIME parameter in sqlnet.ora. The value specified in in minutes.

2) If you control the connection, then there are plenty of option. If you don’t (in case of third party libraries), then TCP KeepAlive may help you. However, Oracle JDBC driver doesn’t provide a good way to customize TCP level parameters. The only way to specify KeepAlive setting is to use TNSEntry. It works with Thin driver. However, on the downside, you need to have connect string in tnsnames.ora. (ENABLE=broken) option will set KeepAlive on TCP socket.
orcl =
(DESCRIPTION =(ENABLE=broken)
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)

System.setProperty("oracle.net.tns_admin", "oracle/NETWORK/ADMIN");
OracleDataSource ods = new OracleDataSource();
ods.setTNSEntryName("orcl");
ods.setDriverType("thin");
ods.setUser("scott");
ods.setPassword("tiger");
ods.setLoginTimeout(2);
Properties cp = new Properties();
cp.setProperty("SetBigStringTryClob", "true");
ods.setConnectionProperties(cp);

A typical error when a connection is not used for extended period is,
java.sql.SQLException: Io exception: Connection reset
at oracle.jdbc.driver.DatabaseError.throwSqlException( DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException( DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException( DatabaseError.java:255)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows( T4CPreparedStatement.java:974 )
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe( OracleStatement.java:1054 )
at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe( T4CPreparedStatement.java:836 )
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout( OracleStatement.java:1124 )
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal( OraclePreparedStatement.java:3285 )

You can keep the JDBC connection alive this way even if its not used for long time. However, if you are in such situation, get out before you employ this trick. You will be happier. Long time back, we ran into such a library and we found out the root cause and realized that we should pull the rip cord.

Blog at WordPress.com.
Theme: Esquire by Matthew Buchanan.

Follow

Get every new post delivered to your Inbox.