cancel
Showing results for 
Search instead for 
Did you mean: 

ClassCastException with EJB

Former Member
0 Kudos

I have two EARs, call them A and B. Both A and B have ejb's inside them; A references B since B contains a ServiceLocator class (j2ee pattern) that A calls.

Whenever I make changes to A and do a build/deploy, I get a ClassCastException when trying to fetch the EJB home object in A. If I stop/start B and re-execute the ejb in A, the ClassCastException does not occur.

Has anyone else run into this issue, and if so, how do you fix it? For obvious reasons, I would rather not have to restart dependant applications when I deploy an application - this defeats the purpose of setting up dependancies in the j2ee engine, since it should be able to unlink/relink the application dependancy chain at runtime based on the sharing references. However, this doesn't appear to be the case.

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Hello All,

this is my init methos, but i received an Exception:

java.lang.ClassCastException: com.sap.examples.calculator.CalculatorHome

public void init() throws Exception {

InitialContext ctx = new InitialContext();

Object ob = ctx.lookup("ejbContexts/sap.com/CalculatorEar/CalculatorEjb.jar/Calculator/java:comp/env/ejb/CalculatorBean");

CalculatorHome home = (CalculatorHome) PortableRemoteObject.narrow(ob,CalculatorHome.class);

calc = home.create();

}

Tell me why i received this Exception, and why my JNDI name

is "ejbContexts/sap.com/CalculatorEar/CalculatorEjb.jar/Calculator/java:comp/env/ejb/CalculatorBean"

normally it sould be "java:comp/env/ejb/CalculatorBean"

Thanks

former_member191569
Active Participant
0 Kudos

Hello Ken,

I also get ClassCastException each time I redeploy my EJB.

The exception is thrown by another application that references the EJB, just in the following line of code:

AccessGPEjbLocalHome home = (AccessGPEjbLocalHome) object;

I resolved the problem by stop/start the EJB EAR in the server (I think the inverse way - did you mean stop/start the application that references the EJB?).

Any other suggestion? What it is hard/soft references useful for?

Former Member
0 Kudos

Hi Ken,

Have you added ejb20.jar in EJB which is giving you ClassCastException?

Regards

Smruti

*Reward points if found useful

Former Member
0 Kudos

Regarding your problem, I had the same trouble with a Webservice that used an EJB. This link was extremely useful to solve my problem. Especially Example 2, which relates to access external EJB apps.

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/0736159e-0301-0010-9ea4-c63...

Hope this helps

former_member182372
Active Contributor
0 Kudos

Hi Ken,

It is class loader issue. When you redeploy ear versionID of class changes and you are getting ClassCastException. What kind of reference do you have between EAR components? hard or weak? I would suggest redeploy all dependant components or create SCA archive which will contain all of them.

Best regards, Maksim Rashchynski.

Former Member
0 Kudos

Maksim,

Well, the issue is related to the dev/deploy/test cycle, so the deployments are manual. The DCs are in the same track, so the 'real' deployment will be done via SCA's, so I'm not worried about dependancy issues.

The references are set as 'hard' right now - I could switch them to 'soft', but I don't think that would solve this dependancy issue. The type of reference controls how applications are started, do they not? Or, would switching to a soft reference help? I'll give it a try and see...

What I've found is that I can stop/start the parent EAR, and this solves the problem. However, it's painful.

I still don't quite understand why deploying a child EAR causes a class cast exception when fetching the parent object, since the parent object hasn't changed. My guess is that the class loader is caching the version ids, and gets confused when the new version of the child ear runs....

BTW, sorry for the long delay in responding - I've been on vacation....