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
- 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 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 normally2.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 normally2.3 If I de-comment a little bit more, when I start playing around with the database, it hangs.
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
- I'm using JDBC to connect to a MySQL Database
- 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