Solved

Checking the validity of an Oracle Connection

Posted on 2004-09-06
10
262 Views
Last Modified: 2012-05-05
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


0
Comment
Question by:jas123
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 92

Accepted Solution

by:
objects earned 168 total points
ID: 11994266
if an error occurs on the connection then it should be considered corrupt and removed from pool.
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 11994287
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").
0
 
LVL 92

Expert Comment

by:objects
ID: 11994296
the isClosed() and getWarnings() methods may help you.
0
 
LVL 7

Assisted Solution

by:grim_toaster
grim_toaster earned 166 total points
ID: 11994304
isClosed won't help, from JavaDoc:

     * This method generally cannot be called to determine whether a
     * connection to a database is valid or invalid.  A typical client
     * can determine that a connection is invalid by catching any
     * exceptions that might be thrown when an operation is attempted.

And a SQL statement is more likely to throw a fatal SQLException for invalid connection than create a warning object.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 35

Assisted Solution

by:girionis
girionis earned 166 total points
ID: 11994309
> If I get a connection form the pool and this connection is no more valid, then the conenction should be reinitialized.

If Oracle is down will you still be able to access the connection pool? In WLS there are exceptions thrown if the db is done while the connection pool was active.

> How I can do this?

Not sure if this will help, but what will happen if you call the isClosed() method on the connection?

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

If the server is restarted then the connection pool should be re-created and initialized, so you shouldn't have any problems.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11994318
> 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.
0
 
LVL 92

Expert Comment

by:objects
ID: 11994332
> 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.


0
 

Expert Comment

by:jerra
ID: 12061750
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
return in catch statement 1 40
topping2 challenge 13 59
GUI builder for Eclipse? 8 10
Java Timer (static) 9 12
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now