Solved

Checking the validity of an Oracle Connection

Posted on 2004-09-06
10
265 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is the latest versions eclipse neon 2 141
create a gui in perl 3 72
get weblogic logged in user in java 2 58
rhino JavaScript import, load 25 69
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 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

23 Experts available now in Live!

Get 1:1 Help Now