Solved

jdbc connection timeout SQL Server not working

Posted on 2009-07-14
17
2,192 Views
Last Modified: 2012-05-07
I am trying to set a connection timeout from Linux/jsp to Windows/SQL Server. I've added "loginTimeout=10;" to my connection string per the driver instructions: https://www.ohprs.org/ohprs/jdbchelp/html/f1b62700-f046-488d-bd6b-a5cd8fc345b7.htm, but it doesn't work. The connection attempt hangs forever waiting on the server if it is down. Am I doing something wrong? Incorrectly? Not enough?

I am using Microsoft SQL Server 2005 JDBC Driver 1.1 on Linux, SQL Server 2005 on Windows
0
Comment
Question by:jmarkfoley
  • 7
  • 5
  • 4
  • +1
17 Comments
 
LVL 14

Expert Comment

by:flob9
ID: 24847649
Try to upgrade to jdbc driver 1.2.  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24847790
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24847988
I downloaded Microsofts 2.0 driver. That does seem to be giving me a timeout (I think. The db server was down during my first test. Now it is back up and I am altering the connect string to give a fake db name). Anyway, the 'catch' code shown below traps the error, but I have a status of 0. The message displayed is:

Sorry, unable to connect with Database Server. Please try again later. Status: 0 The connection to the host server, named instance traverse106 has failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names, check that no firewall is blocking UDP traffic to port 1434, and for SQL Server 2005 or later verify that the SQL Server Browser Service is running on the host.

I'm not sure why it's returning a zero status. Should I be trying to trap a different exception? I could arbitrarily do a System.exit() with a made-up status, but I'd rather exit with the correct error.

Thots?

    try {

        Class.forName( dbDriver );
 

        con = DriverManager.getConnection( dbURL );

    }

    catch (SQLException e) {

        System.out.println("Sorry, unable to connect with Database Server. " +

            "Please try again later. Status: " + e.getErrorCode() + " " +

            e.getMessage());

        System.exit(e.getErrorCode());

    }

    catch (ClassNotFoundException x) {

        System.out.println(x);

    }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24848011
You can only really work with the code you're given. Zero could be correct though
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24848015
(although not correct for System.exit of course)
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24848220
what if I try catching java.net.SocketTimeoutException? but it seems to me it would do the catch logic for SQLException if there were a SOcketTImeoutException
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24848240
Nope. Trying to use SocketTimeoutException gives me:

nasql.java:136: exception java.net.SocketTimeoutException is never thrown in body of corresponding try statement
    catch (SocketTimeoutException x) {
    ^
1 error

so, zero is it, eh?
0
 
LVL 14

Expert Comment

by:flob9
ID: 24848263
Yes you should catch the socketexception. The sql error code does not means anything since the problem occur before connection to the database.

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 14

Expert Comment

by:flob9
ID: 24848333
You can catch the default Exception after the ClassNotFoundException ...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24848339
What does the following give you?

a. System.out.println(e.getClass());

b. System.out.println(e.getCause().getClass());
0
 
LVL 14

Expert Comment

by:flob9
ID: 24848411
Also check e.getSQLState() and e.getNextException()
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24848527
Probably a good idea to cross-post the question to the SQL Server zone next time.

Have you configured the SQL Server to listen for network connections? Out of the box it does not without a config change.

If not sure: Run SQL Server Configuration Manager -> SQL Server 2005 Network COnfiguration -> Protocols for {DB} -> Double click TCP/IP
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24851742
There seems to be lots of good suggestions here ...

mrjoltcola, the SQL server connection works fine normally. The application at hand is a java program run from a bash shell as a scheduled (cron) job. I'm just trying to catch those rare instances where the server is down so I can exit with an appropriate code to prevent continuation of the down-stream bash script.

 CEHJ/flob9, my try/catch code is shown above in post 24847988. where do I stick your recommended System.out.println(e.getClass()); System.out.println(e.getCause().getClass()); e.getSQLState() and e.getNextException() ? In the catch (SQLException e) block? When I do that, System.out.println(e.getClass()): gives me:
class com.microsoft.sqlserver.jdbc.SQLServerException

System.out.println(e.getCause().getClass()); give me: nullPointerException

System.out.println(e.getSQLState()); gives me: 08S01

Not quite sue what to do with e.getNextException(). doing ...println() just gives me 'null'.

flob9, when I stick: catch (Exception q) { System.out.println(q); } after the class not found exception  per your suggestion in 24848333 it never goes there. It is still trapped in the SQLException
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24851886
You just need to use (preferably) a unique id for that error. getErrorCode or getSQLState should be OK. Then exit the app
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24853688
OK, I'll just do:  System.exit((e.getErrorCode() == 0) ? -1 : e.getErrorCode()));

I suppose that's the best I can do? Other thots, anyone?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24853885
Well there could be other exceptions where you would want to exit the script so you should probably be thinking about that too
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24854288
:-)
0

Featured Post

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.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

743 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

15 Experts available now in Live!

Get 1:1 Help Now