问题描述
我正在尝试将JNDI与名为CEDataSource的自定义数据源一起使用.根据我的理解,我还必须创建一个自定义工厂.
I am attempting to use JNDI with a custom DataSource called CEDataSource. From my understanding for this to work I would have to create a custom factory as well.
所以我创建了一个自定义工厂,该工厂将返回CEDataSource对象,但是现在当我尝试在Java中使用此对象时
So I created a custom factory that would return the CEDataSource object but now when I attempt to use this in Java with
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
CEDataSource ds = (CEDataSource)envCtx.lookup("jdbc/cePu");
我收到异常ClassCastException"CEDataSource无法映射到CEDataSource".我将CEDataSource和CEDataSourceFactory添加到TOMCAT/lib文件夹中,并在已部署的应用程序中引用了这个jar.
I get the exception ClassCastException"CEDataSource cannot be mapped to CEDataSource". I added the CEDataSource and the CEDataSourceFactory to the TOMCAT/lib folder as well as referenced this same jar on my deployed application.
对于为什么会出现此错误的任何帮助,将不胜感激.谢谢
Any help would be greatly appreciated on why this possible error may occur. Thanks
推荐答案
"CEDataSource无法映射到CEDataSource"似乎是指两个地方的CEDataSource都不相同.
"CEDataSource cannot be mapped to CEDataSource" seems to point to the fact that it's not the same "CEDataSource" in both places.
类加载器可能有所不同,如果在多个位置具有相同的jars/.class,通常会发生这种情况.
What could be different is the classloader and this usually happens if you have the same jars/.class(es) in multiple locations.
您有多个jar副本吗?尝试在共享的tomcat lib中创建一个副本,以便无论您从何处访问它,都由同一个类加载器加载.
Do you have multiple copies of your jar?Try to have a single copy, maybe in the shared tomcat lib so it's loaded by the same classloader no matter from where you access it from.
这篇关于JNDI ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!