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.

