Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unfortunate Java Exception: java.lang.NoSuchMethodError

I wrote an application, an it was working fine for 3 years, but! today when they try to run this application, an unexpected exception raised:

INFO   | jvm 1    | 2013/04/17 10:02:40 | Exception in thread "Thread-1" java.lang.NoSuchMethodError: java.sql.Connection.isValid(I)Z
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at lib.MySQLConnectionPatch.SearchInCache(MySQLConnectionPatch.java:103)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at lib.MySQLConnectionPatch.getConnection(MySQLConnectionPatch.java:79)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at lib.SQLManager.establishSqlConnection(SQLManager.java:62)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at lib.SQLManager.establishSqlConnection(SQLManager.java:30)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at lib.tasks.classes.sql.executeQuery.execute(executeQuery.java:49)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at Components.TTask.run(TTask.java:86)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at Components.ThreadTask.run(ThreadTask.java:29)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at lib.tasks.classes.fori.execute(fori.java:66)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at Components.TTask.run(TTask.java:86)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at Components.ThreadTask.run(ThreadTask.java:29)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at lib.tasks.classes.fori.execute(fori.java:66)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at Components.TTask.run(TTask.java:86)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at Components.ThreadTask.run(ThreadTask.java:29)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at Components.ThreadTask.run(ThreadTask.java:44)
INFO   | jvm 1    | 2013/04/17 10:02:40 |   at LauncherService.LaunchService.run(LaunchService.java:38)

code of MySQLConnectionPatch.java/SearchInCache is

private Connection SearchInCache(String key) {

    Connection result = null;
    try{
        if (!connections.isEmpty())
            result  = connections.get(key);
    } catch (Exception ex){
    }
    if (result != null){
        boolean isValid = false;  /** THIS IS LINE 103 WHERE EXCEPTION RAISED **/
        try {
            Statement s = result.createStatement();
            if (s.execute("SHOW STATUS;")){
                isValid = true;
            }
            s.close();
        } catch (Exception ex) {
            isValid = false;
        }

        if (!isValid){
            connections.remove(key);
            messageLog.stdwar("MySQL_PATCH: ONE CONNECTION EXPIRED :: force close");
            try {
                result.close();
            } catch (SQLException ex) {
                messageLog.stderr("MySQL_PATCH: CLOSING CONNECTION ERROR: "+ex.getMessage());
            }
            result = null;
        }
    }
    return result;
}

I wonder why boolean isValid = false; raise Exception java.lang.NoSuchMethodError: java.sql.Connection.isValid(I)Z.

Note that the only thing differs is JRE (former was 1.6, and the new is 1.7)

like image 460
Mostafa Nazari Avatar asked Apr 17 '13 06:04

Mostafa Nazari


2 Answers

Odd. It's clearly referencing the isValid(int) method in java.sql.Connection

In your stacktrace I also see an I and a Z: java.sql.Connection.isValid(I)Z

Those correspond to int (I) and boolean (Z), the exact signature for the method in java.sql.Conneciton. So a method is definitely being called. Note: the character to the right of the parentheses indicates the method's return type, and this method returns boolean (Z).

Here are the only ideas I can think of:

  1. The source code you presented does not correspond to what's actually running in your environment. (e.g., maybe your "patch" is never really applied?)

  2. Also, your environment might be running Java 5, since the isValid(int) method does not show up until Java 6.

like image 65
Julius Musseau Avatar answered Oct 04 '22 04:10

Julius Musseau


Sorry, I can't comment.

Did you just replace the jvm and none of your class-files? Please try your sources in an IDE (remember to replace also dependencies etc), so that you sources are recompiled and know about the new signatures that came with 1.7.

For the rest, I go with Julius Davies. Java should call isValid() (would be interesting, what happens, if you change it to boolean nameOtherThanAMethodOfConnection = false;).

like image 26
Sammy Avatar answered Oct 04 '22 03:10

Sammy