Solved

jdbc connection timeout SQL Server not working

Posted on 2009-07-14
17
2,203 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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
 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

770 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