This discussion is archived
10 Replies Latest reply: Feb 25, 2013 12:39 PM by 939520 RSS

Connecting to SQL server hangs forever.

user10878887 Newbie
Currently Being Moderated
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.
  • 1. Re: Connecting to SQL server hangs forever.
    gimbal2 Guru
    Currently Being Moderated
    You're not even asking a question, you're just making a statement. I don't have your problem in any case.
  • 2. Re: Connecting to SQL server hangs forever.
    939520 Explorer
    Currently Being Moderated
    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.
  • 3. Re: Connecting to SQL server hangs forever.
    user10878887 Newbie
    Currently Being Moderated
    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...
  • 4. Re: Connecting to SQL server hangs forever.
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    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.
  • 5. Re: Connecting to SQL server hangs forever.
    Bernd Eckenfels Newbie
    Currently Being Moderated
    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.
  • 6. Re: Connecting to SQL server hangs forever.
    user10878887 Newbie
    Currently Being Moderated
    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.
  • 7. Re: Connecting to SQL server hangs forever.
    Bernd Eckenfels Newbie
    Currently Being Moderated
    Did you wait at least 15 minutes for a timeout?
  • 8. Re: Connecting to SQL server hangs forever.
    user10878887 Newbie
    Currently Being Moderated
    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();
  • 9. Re: Connecting to SQL server hangs forever.
    user10878887 Newbie
    Currently Being Moderated
    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.");
        }
    }
  • 10. Re: Connecting to SQL server hangs forever.
    939520 Explorer
    Currently Being Moderated
    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.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points