Link to home
Start Free TrialLog in
Avatar of jas123
jas123

asked on

Checking the validity of an Oracle Connection

HI,

I use Oracle JDBC Driver's OracleConnection class in one of my applications where I have implemented connection pooling.

Now I faced a problem once. It is as follows:

When I once shutdown the oracle server when my application was up, I was unbale to get data from the databse, which is correct.

But ideally, the connection pool should not be affected in such circumstances.

If I get a connection form the pool and this connection is no more valid, then the conenction should be reinitialized.

How I can do this?

How do I know(in my ocde) that the oracle server is restarted?

Thanks
Regards
Jas


ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of grim_toaster
grim_toaster

Some application servers (i.e. weblogic) have an additional step that may be worth considering, they have a test connection on reserve against a well-known table that does exist, if any error occurs then the connection is considered invalid (for Oracle the test query would be "SELECT 1 FROM dual").
the isClosed() and getWarnings() methods may help you.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> Some application servers (i.e. weblogic) have an additional step that may be worth considering, they have a test
>connection on reserve against a well-known table that does exist, if any error occurs then the connection is considered
>invalid (for Oracle the test query would be "SELECT 1 FROM dual").

Yes, this one also. I completely forgot about this.
> A typical client
> * can determine that a connection is invalid by catching any
> * exceptions that might be thrown when an operation is attempted.

yes as i originally said, if the connection fails then it should be removed from pool.


I made a CachedConnectionFactory class based on the Oracle classes OracleConnectionCacheImpl and OracleConnectionPoolDataSource.

In my CachedConnectionFactory.getConnection() method I use:

      try {
        tmpConnection = oracleConnectionCacheImpl.getConnection();
      }
      catch (SQLException ex1) {
        logger.error("SQLException while getting connection.",ex1);
        resetConnectionFactory();
        tmpConnection = oracleConnectionCacheImpl.getConnection();
      }

In resetConnectionFactory() I close the oracleConnectionCacheImpl object and create a new one. This works just fine for me.
If the second try does not work (SQLException) then the routing returns null.