Home > Blockchain >  ClassCastException with ECIConnectionFactory running in Liberty
ClassCastException with ECIConnectionFactory running in Liberty

Time:11-23

The problem - I am receiving the following message:

java.lang.ClassCastException: com.ibm.connector2.cics.ECIConnectionFactory incompatible with com.ibm.connector2.cics.ECIConnectionFactory

I am receiving it when trying to make the following statement:

eisDci = (ECIConnectionFactory)ctx.lookup(eisn);

The 'eisDci' has been defined previously: private static ECIConnectionFactory eisDci = null;

And the 'eisn' is the String with the name of the conection like 'eis/DCIXxxxECI'

These connection is defined in the Server.xml:

<connectionFactory id="DCIXxxxECI" jndiName="eis/DCIXxxxECI">
        <properties.cicseci ServerName="XXXX" TPNName="xx" connectionUrl="url" portNumber="2006"/>
</connectionFactory>

I understand that this is warning me that the cast is not possible. What I don't know is what I'm doing wrong. That must be comparing one version of the ECIConnectionFactory class with a different version of ECIConnectionFactory.

The server I'm working with is a Liberty, I'm going crazy, I can't figure out why Eclipse is comparing two different versions.

Similar problems I have searched for:

ClassCastException when casting to the same class

Waxwing's answer seems good, but I don't have access to make those changes, This connection is carried out by an external library.

First Thank you for your answer Ben Cox, in Liberty's server.xml (for LOCAL) I have declared the library:

<fileset caseSensitive="false" dir="C:\CICSECI"/>

And in the Liberty Runtime/Shared/resources I have cicseci.rar which I have declared in the server.xml as a resourceAdapter:

<resourceAdapter autoStart="true" id="cicseci" location="${shared.resource.dir}/cicseci.rar">
    <classloader apiTypeVisibility="spec, ibm-api, api, third-party"/>
</resourceAdapter>

I have checked the rest of the libraries that I am importing into the project, and so far I have not seen that I have the repeated library.

CodePudding user response:

I think the issue here has to do with classloading between your resource adapter and your application. This is something we more commonly see with DataSources but the results are the same.

The problem is that the same jar is being loaded by two different classloaders. One classloader for the resource adapter, and another classloader for your application. The solution is to use a commonLibraryRef

<library id=cicseci>
  <file name="${shared.resource.dir}/cicseci.rar"/>
</library>

<resourceAdapter autoStart="true" id="cicseciRA">
    <classloader commonLibraryRef="cicseci"/>
</resourceAdapter>

<connectionFactory id="DCIXxxxECI" jndiName="eis/DCIXxxxECI">
        <properties.cicseci ServerName="XXXX" TPNName="xx" connectionUrl="url" portNumber="2006"/>
</connectionFactory>

<!-- Location of app that is trying to cast ECIConnectionFactory -->
<application location="${shared.resource.dir}/cicseci.rar">
  <classloader commonLibraryRef="cicseci"/>
</application>

In this configuration, the cicseci.rar will only be loaded once.

CodePudding user response:

ClassCastException can happen when the same class is loaded by two different class loaders, making what otherwise looks like the same class incompatible.

The mechanism that you should be using to avoid this involves configuring the application's class loader with a classProviderRef to the resourceAdapter, which is documented here. For example,

<application location=...>
  <classloader classProviderRef="cicseci"/>
</application>
  • Related