954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Create timeout for Oracle DB connection attempt

I am trying to determine a way to create a timeout for a connection attempt to remote Oracle databases. If the connection isn't created in X seconds or less, regardless of the reason, I need to stop the connection attempt and report a message. Below is the relevant bit of code... Could of you suggest a way that I can implement such a timer on the connection attempt?

...
   String entry = "jdbc:oracle:thin:@" + host + ":" + port + ":" + sid;
   ... 

   Connection con=null;
   Class.forName("oracle.jdbc.driver.OracleDriver");
   con=DriverManager.getConnection(entry,"USERID","PWD");
   Statement s=con.createStatement();
   s.execute(sql);
   s.close();
   con.close();
echocoder
Newbie Poster
8 posts since Oct 2007
Reputation Points: 8
Solved Threads: 0
 

You should be able to set the timeout with this method prior to any calls to getConnection: DriverManager.setLoginTimeout(int)

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Thanks for the solution Ezzaral. It works great!

echocoder
Newbie Poster
8 posts since Oct 2007
Reputation Points: 8
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You