0

UPDATE NOTE: After Debugging, I asked another question with the results: Class Constructor fails throwing Exception on Class Loading

First, I could not think of a better name for my question, what I'm about to ask is just that: WEIRD BEHAVIOR. If you think of something more appropriate please edit.

Now, I'm going to try to simplify this as much as I can, sadly I can't give away the code which is giving me the problems.

I have a Class A, a Class B and a Class Z.

Class A and B are very very similar, both have a constructor with the same params, and each have a method which invokes a SOAP web service, one to do Operation A, and the other to do Operation B.

Now,what's the problem? Class Z instantiates both Class A and Class B and then on someMethod() calls the object's method to do Operation A and B respectively.

For some reason the constructor of ClassB doesn't seem to be called, and no System.out.println() prints anything from the moment the Constructor is called, the program hangs forever, i mean it, it never throws and exception or continues to do anything. Look how on the first line of Class A and Class B I print a flag, it does not print for ClassB.

WHAT I'VE TRIED

  1. On ClassZ I changed the order for Class A and B -> RESULTS: It hangs on the same place and now not even Class A constructor and method gets called
  2. On ClassB I commented everything, and started de-commenting each instruction one by one, starting with the Constructor and then with MethodB, here's where I got stuck and came here, because what I found makes no sense for me nor anybody at my workplace. RESULTS ->

    2.1 If I comment everything on methodB but the return false in the end and leave the Constructor as is, it continues execution normally

    2.2 If I de-comment everything on 2.1 plus just the part from methodB where it invokes the operationB from the WebService and checks it's result, it continues execution normally

    2.3 If I de-comment a little bit more, when I start playing around with the database, it hangs.

  3. I checked the number of Connections to the Database, it still has many available

What has me confused is, if it's something Database related, why does it hangs like that at the constructor?

GENERAL FACTS

  1. I'm using JDBC to connect to a MySQL Database
  2. I have everything around try{}catch(Exception e){} and I'm printing everything on e, but it just doesn't print anything, no exception is thrown.

Here's what they look like:

CLASS A:

// ClassA.java

public ClassA{
    private UserInfo user;
    private WebServiceADelegate port;
    private Connection conn;

    public ClassA (UserInfo user, Connection conn) throws Exception {
        System.out.println("CLASS A CONSTRUCTOR");
        this.user = user;
        this.conn = conn;
        this.port = new WebServiceAService().getForwardingPort();
    }

    public boolean methodA(List<String> list){
        // Check some stuff on database using this.conn
        // Get the values to invoke SOAP service using this.conn
        status = port.operationA(values);
        if(status > 0)
            return true;
        return false;
    }

}

CLASS B:

// ClassB.java

public ClassB{
    private UserInfo user;
    private WebServiceBDelegate port;
    private Connection conn;

    public ClassB (UserInfo user, Connection conn) throws Exception {
        System.out.println("CLASS B CONSTRUCTOR");
        this.user = user;
        this.conn = conn;
        this.port = new WebServiceBService().getForwardingPort();
    }

    public boolean methodB(List<String> list){
        // Check some stuff on database using this.conn
        // Get the values to invoke SOAP service using this.conn
        status = port.operationB(values);
        if(status > 0)
            return true;
        return false;
    }

}

CLASS Z:

// ClassZ.java

public ClassZ{
    private Connection conn;
    // ...        
    // ...
    public boolean someMethod (){
        System.out.println("GONNA CALL CONSTRUCTOR ClassA");
        ClassA webservice = new ClassA(user, conn);
        System.out.println("GONNA CALL METHOD FROM ClassA");
        if (!webservice.methodA(list) ){
            return false;
        }

        System.out.println("GONNA CALL CONSTRUCTOR ClassB");
        ClassB webservice2 = new ClassB(user, conn);
        System.out.println("GONNA CALL METHOD FROM ClassB");
        if (!webservice2.methodB(list) ){
            return false;
        }
        return true;
    }
}

The last output when it hangs it's always: "GONNA CALL CONSTRUCTOR ClassB"

Thanks!

UPDATE:

I debugged my app and got to fix it, however it's still really strange. Tomorrow I'll elaborate more to see if somebody can tell me what was going on.

Long story short: I was importing javax.persistence.NoResultException on ClassB, and on some point inside methodB i was doing try{}catch(NoResultException nre){ //...}… For some reason when the JVM was calling the ClassLoader before calling the Constructor it was throwing the aforementioned Exception and the behavior from that point was just weird and the execution ended.

There are threads involved, that's why I though it hang, it didn't hang, the Thread ended and I didn't notice.

An additional note: ClassB IS NOT using JPA, and the import as well as the catch for it were incorrectly there, some really old code that managed to survive. However I think it doesn't justify the error.

I asked another question, one with the findings of the Debugging. You might check it in Class Constructor fails throwing Exception on Class Loading

Community
  • 1
  • 1
hectorg87
  • 753
  • 1
  • 7
  • 24
  • 2
    Go with a debugger and tell us where it hangs. When I should bet then I would say that some call to the webservice hangs which does not return. But better you tell us on what line of code your program hangs. – Fabian Barney Jul 30 '12 at 17:13
  • @FabianBarney Well I surely haven't tried with a debugger, the test environment I'm using is anything but good. I'll see what I can do tomorrow, but from my flags I'd say it hangs on the `new ClassB(user, conn);` – hectorg87 Jul 30 '12 at 17:16
  • I would say that you are behind a **proxy** and when you create the WebService it is trying to reach the WSDL **without proxy** configuration hanging your application ;) – Francisco Spaeth Jul 30 '12 at 17:18
  • Does it hang forever? Have you left it say 5 mins to see what happens? – DaveH Jul 30 '12 at 17:18
  • @FranciscoSpaeth The WSDL is reachable from the machine, I ping the IP and also download the WSDL with wget and there's no problem. Thanks, I'll add that to the question. Also, notice that ClassA does work without problem, and the WSDL is on the same server as the one from ClassB – hectorg87 Jul 30 '12 at 17:19
  • @DaveHowes I have left it for 30 minutes with no response. – hectorg87 Jul 30 '12 at 17:20
  • could you reach it with: `curl -x "" "wsdl address"`? -x removes the proxy, because wget could take default proxy configurations... – Francisco Spaeth Jul 30 '12 at 17:21
  • @FranciscoSpaeth `curl -x "" "wsdl address"` had success. – hectorg87 Jul 30 '12 at 17:22
  • strange, from my point of view its time to execute a debugging ;) – Francisco Spaeth Jul 30 '12 at 17:27
  • @FranciscoSpaeth I'm on it. I'll come back with the results. – hectorg87 Jul 30 '12 at 17:29
  • @FranciscoSpaeth I just edited my question with an update. I found the problem and fixed it, however I still don't understand what's going on. – hectorg87 Jul 30 '12 at 18:49
  • @FranciscoSpaeth I asked another question, one with the findings of the Debugging. You might check it in http://stackoverflow.com/questions/11738383/class-constructor-fails-throwing-exception-on-class-loading – hectorg87 Jul 31 '12 at 10:52

2 Answers2

1

Well, hard to say with the infos provided, but some hints what I would check:

  1. First use a debugger and step into the constructor whatever to be really sure where it hangs. Knowing it's the constructor call is not enough. Step into it.
  2. There is a good chance that a webservice call hangs. So you call a webmethod and it does not return forever. So your program hangs forever.
  3. The hang can be anything but when you deal with databases there is a good chance that the hang comes from a database lock. You can easily produce dead locks here when you do not commit/rollback properly.

Some wild guesses but maybe it helps ...

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60
  • Thanks Fabian, 1. I did, check my UPDATE on the question. 2. I checked everything related to the WS, in the worst case it would throw a timeoutException. 3. No, the database was operating ok, if there's a deadlock MySQL jdbc driver throws an Exception because MySQL can detect the deadlocks (at least on InnoDB Engine), been there. – hectorg87 Jul 30 '12 at 19:26
  • MySQL can only detect db internal dead locks. It is still possible that your program waits for a current statement that needs a lock that you should have better released before on another connection. But this is not released, because your program hangs on a statement that needs the lock... – Fabian Barney Jul 30 '12 at 20:07
  • And the WS does not necessarily timeout when there's a connection keep-alive. – Fabian Barney Jul 30 '12 at 20:12
  • I asked another question, one with the findings of the Debugging. You might check it in http://stackoverflow.com/questions/11738383/class-constructor-fails-throwing-exception-on-class-loading – hectorg87 Jul 31 '12 at 10:51
1
  1. The application didn't hang forever. This piece of code was running in another thread so when its execution failed it finished without me noticing.
  2. The problem was in fact when calling the constructor. I didn't have the class javax.persistence.NoResultException on my classpath so the JVM ClassLoader couldn't find it when creating the instance and when checking for dependencies it crashed.
  3. Finding out what was the problem was difficult even debugging because I had a lot of try{}catch(Exception e){} but even though the ClassLoader.loadClass() of the JVM ClassLoader says it throws an Exception (ClassLoader.loadClass() signature is: public Class<?> loadClass(String name) throws ClassNotFoundException) it was actually throwing a NoClassDefFoundError, which DOES NOT EXTEND EXCEPTION, it extends Throwable, and therefore was not being catch by my catch(Exception e){}, I had to do catch(Throwable t){}.
  4. As of why was it throwing something different, it's because how Java's internals class loading works. It's a bit more complicated than just calling loadClass. It has to load, link, verify and initialize it. At some point it is supposed to throw a NoClassDefFoundError when calling defineClass(). More on this can be found on the Execution part of the JLS

For a more detailed answer on what was heppening, check my other question regarding this issue after debugging and the accepted answer Class Constructor fails throwing Exception on Class Loading

Community
  • 1
  • 1
hectorg87
  • 753
  • 1
  • 7
  • 24