Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / October 2006

Tip: Looking for answers? Try searching our database.

How test Connection not valid?

Thread view: 
Paolo - 29 Sep 2006 17:52 GMT
Hello,
I wrote a simple java program, where a Connection is created at the
beginning, and than is used to make queries to an Oracle DB (let me say
"a very very primitive connection pool").

When I run this program on a PC with only one ethernet card and
configured with only one LAN, if I disconnect LAN cable while program
is waiting "Press any key (q to exit)... " an than press a key, an
java.sql.SQLException (Connection reset by peer: socket write error) is
thrown.

But when I run this program on a PC with two ethernet card and
configured with two LAN (all active), if I disconnect LAN cable
(Connection created with this LAN) while program is waiting "Press any
key (q to exit)... " an than press a key, nothing happens, and program
wait forever!

I tried Oracle thin driver classes12.jar and ojdbc14.jar, but
conn.pingDatabase(10) returns everytime 0.

How can I test a Connection before use it?

Below a part of my code.

Thanks,
Paolo

try {
           conn = DriverManager.getConnection(url, user, pwd);
           BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));
           System.out.print("\nPress any key (q to exit)... ");
           while(!stdin.readLine().equalsIgnoreCase("q")){
               st = conn.createStatement();
               rs = st.executeQuery("select * from tab");
              while (rs.next()) {
               System.out.println(rs.getString("TNAME") + " - " +
rs.getString("TABTYPE"));
              }
             rs.close();
             st.close();
             System.out.print("\nPress any key (q to exit)... ");
           }
       }
joeNOSPAM@BEA.com - 29 Sep 2006 20:33 GMT
> Hello,
> I wrote a simple java program, where a Connection is created at the
[quoted text clipped - 12 lines]
> key (q to exit)... " an than press a key, nothing happens, and program
> wait forever!

For some network failures like disconnecting the network, the low level
socket read will hang for minutes until the TCP timeout limit is
reached
(typically 5 minutes). For most failures, running a quick query like
select 1 from dual will work fine with a quick exception if the
connection
is dead, but there's nothing else you can do to verify whether a
connection
is still good.
Joe Weinstein at BEA Systems
kuassi.mensah@gmail.com - 01 Oct 2006 00:13 GMT
I am aware that you might not be looking for a solution based on the
connection pool but the 10g JDBC connection pool (Implicit Connection
Cache) lets you validate the state of a connection it retrieves from
the pool before handing it to the application. You just need to set
ValidateConnection property to true. Check the JDBC 10g doc or chapter
7 of my book.

Kuassi.

> Hello,
> I wrote a simple java program, where a Connection is created at the
[quoted text clipped - 40 lines]
>             }
>         }
dohunt@gmail.com - 19 Oct 2006 23:02 GMT
Has anyone tried this setting (setValidateConnection) on an
OracleDataSource and observed the behaviour in a scenario where the
connection would have otherwise hung on the TCP timeout?  I'm looking
for a way to never hit the timeout on 'new' connections from the pool
(one way is doing it in application and setting 1 second transaction
timetout).  I'd like to write as little code as possible so if this
setting works I'll go for it.

I will test this myself but i'm in design stage and just curious.
thanks!

> I am aware that you might not be looking for a solution based on the
> connection pool but the 10g JDBC connection pool (Implicit Connection
[quoted text clipped - 49 lines]
> >             }
> >         }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.