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.

Advertisements

5 thoughts on “Oracle JDBC Connection KeepAlive

  1. Hello there! I could have sworn I’ve visited this website before but after browsing through many of the articles I realized it’s new to me.

    Anyhow, I’m certainly happy I discovered it and I’ll be book-marking it and checking back frequently!

  2. Pingback: Mantener viva una conexión JDBC de Oracle | El Blog de rubensa

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: