Forum Stats

  • 3,827,710 Users
  • 2,260,808 Discussions
  • 7,897,356 Comments

Discussions

Connecting to SQL server hangs forever.

user10878887
user10878887 Member Posts: 77
When I execute this line
Connection conn = DriverManager.getConnection(url, user, password);
in java 1.7.013 the application hangs forever if the username, password or database name are incorrect.

If the username, password or database name are correct then the connection works fine.
There should be a message in the first case(wrong password...).
I tested this for com.mysql.jdbc.Driver and org.postgresql.Driver.

Regards,
Victor S.

Answers

  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    edited Feb 17, 2013 9:13AM
    You're not even asking a question, you're just making a statement. I don't have your problem in any case.
  • 939520
    939520 Member Posts: 186
    edited Feb 18, 2013 8:54PM
    What does your catch (exception) statement look like? Within it are you printing the stack trace, ie: e.printStackTrace()? if not, you may be just consuming the exception that is thrown and not bothering to print anything out, and therefore not being notified, therefore it only looks like it hangs.
    939520
  • I have try and catch.
    I tested this problem in a new Netbeans project and I could not reproduce the problem.
    So there is something related my original context...
  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    user10878887 wrote:
    I have try and catch.
    That wasn't the question.

    Are you printing the stack trace in the catch block?

    Are you catching Throwable (not Exception)?
    So there is something related my original context...
    What it means is that it is really likely that your code is eating the exception and/or then continuing in an unexpected way.
  • Hanging (for multiple minutes) if the host from the url is unreachable or the port is filtered is likely. It can also be that the database/listener is wrongly configured (and returns a wrong redirected address). But wrong user or wrong password hanging sounds very unlikely (maybe DB firewall?). So it seems more like a problem with the error handling or retry in your application code. Escpecially since it happens with 2 entirely different database drivers.

    BTW: a quick diagnostic approach (besides posting your code so we can comment) would be to use an debugger or maybe Linux strace.
  • user10878887
    user10878887 Member Posts: 77
    edited Feb 24, 2013 1:25AM
    Yes, I print the stack trace.
    This is the code that is hanging:
                try { 
                    System.out.println("Start create a new connection");
                    Connection conn = DriverManager.getConnection(url, user, password); 
                    System.out.println("Created a new connection - created for catalog "+conn.getCatalog());
                } catch (SQLException ex) {
                    ex.printStackTrace();
                    Logger.getLogger(JDCConnectionPool.class.getName()).log(Level.SEVERE, null, ex);
                    return null;
                }
    And the result is "Start create a new connection".
    The execution never arrives to the line "Created a new connection".

    When I created a new project like this:
    public class ConnTest {
        public static String url = "jdbc:mysql://10.116.84.126/adminDB?useUnicode=true&characterSetResults=UTF-8&characterEncoding=UTF-8&useUnicode=yes";
        public static String user  = "www";
        public static String password  = "www";
    
        public static void main(String[] args) {
            try {
                Connection conn = DriverManager.getConnection(url, user, password);
            } catch (SQLException ex) {
                Logger.getLogger(ConnTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    an error appeared when user, db or password were wrong.
  • Did you wait at least 15 minutes for a timeout?
  • user10878887
    user10878887 Member Posts: 77
    edited Feb 24, 2013 10:33AM
    I just waited for 30 minutes and still no message.
    But this is not a problem because the server is answering too late.
    As someone suggested earlier the exception must be somehow consumed without beeing printed.

    However, all the code has try and catch and all catch-es have ex.printStackTrace();
  • The hanging code is part of a connection pool and the getParentLogger function is not implemented.
    I think this is the problem.
    How should I get the parrent logger to implement this function?
    public class JDCConnectionDriver implements Driver {
    ...
        @Override
        public Logger getParentLogger() throws SQLFeatureNotSupportedException {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
  • 939520
    939520 Member Posts: 186
    You can also try using a database uitlity such as Toad to see if you can connect independent of your application. you can also try to restart your database.
This discussion has been closed.