ClassCastException doing JNDI lookup

Hi there,

I'm trying to migrate a couple of J2EE apps from Orion over to SJSAS and having a few problems.

I'm using JNDI to share a ConnectionFactory object between both apps. The factory has a static getFactory() method that checks to see if it's already in JNDI, and if not creates a new one and binds it. If it is in JNDI then it maintains a local reference to it.

publicstatic ConnectionFactory getFactory()throws ConnectionFactoryException

{

if(logger.isDebugEnabled()) logger.debug("ConnectionFactory.getFactory called");

if (ConnectionFactory.aFactorySingleton ==null)

{

try

{

ConnectionFactory factory;

// first check to see if it is in JNDI

Context ctx =new InitialContext();

if(logger.isDebugEnabled()) logger.debug("Checking to see if factory is in jndi");

try

{

factory = (ConnectionFactory)ctx.lookup(JNDIFACTORYNAME);

if (factory !=null)

{

// if it already exists then set the local static parameter

if(logger.isDebugEnabled()) logger.debug("Factory is already in JNDI so using existing ref");

ConnectionFactory.aFactorySingleton = factory;

}

}

catch (NamingException e)

{

// if not then create a new one and put it in the JNDI tree

if(logger.isDebugEnabled()) logger.debug("ConnectionFactory does not exist in JNDI so binding new instance");

factory =new ConnectionFactory();

ctx.bind(JNDIFACTORYNAME, factory);

// set the local static parameter

ConnectionFactory.aFactorySingleton = factory;

}

}

catch (NamingException e)

{

logger.error("Naming exception setting Connection factory in JNDI");

thrownew ConnectionFactoryException(e);

}

}

return ConnectionFactory.aFactorySingleton;

}

Now this works fine in Orion, running both apps from the same server instance. Running it using SJSAS gives a ClassCastException if i try and call getFactory() from the application that did not create and bind the Factory.

I'm using a seperate virtual server for each application as they need to be on different ports, so i'm wondering if that might be causing the problem with the JNDI lookup. I've tried just doing a lookup from the second app - rather than calling getFactory() and that is giving me the same ClassCastException error.

Context ctx =new InitialContext();

ConnectionFactory factory = (ConnectionFactory)ctx.lookup(JNDIFACTORYNAME);

Can different virtual servers share the same JNDI resource by default? Or are there configuration changes i need to make.

Any help on this would be greatly appreciated as i'm totally stumped...

Regards, Ed.

[4070 byte] By [mrEdmundo] at [2007-11-26 6:55:52]
# 1

UPDATE:

On further investigation i'm thinking that the ClassCastException may be caused by having different ClassLoaders.

The SMSCConnectionFactory is definately in JNDI and I can perform a lookup on it, i just can't cast it. I've got resources.jar files i use to store common classes that are shared between apps, but as it's intended that these apps could be deployed seperately they have their own copy of the resources.jar in each ear file.

I've tried moving the call to SMSCConnectionFactory.getFactory() into an EJB, so that it's in the EJB classLoader but it's still giving the same error.

It was created initially in a Servlet in one of the apps, then put into JNDI. It's also serializable.

Any ideas on what to do now...? Any help would be greatly appreciated.

Ed.

mrEdmundo at 2007-7-6 15:29:10 > top of Java-index,Application & Integration Servers,Application Servers...