Websphere 6.1,数据源,两个应用程序
我有一个在 Websphere 6.1 服务器上运行的 Web 应用程序,它通过 JNDI 查找数据源。这个 web 应用程序运行良好,Websphere 服务器识别 web.xml 中的数据源引用等。
现在我们添加了第二个 web 应用程序,它应该使用具有相同配置的数据源,但是 当我尝试访问数据源时,我收到一个 ClassCastException
。
为了找到问题,我将其放入我的代码
try{
InitialContext ctx = new InitialContext();
Object obj = ctx.lookup(N2WebConstants.datasourceJNDI);
System.err.println(obj.toString());
System.err.println(obj.getClass());
con = ((DataSource) obj).getConnection();
}catch (Exception e){
System.err.println(e);
System.err.println(e.getCause());
}
中 在打印的错误日志中
[5/10/10 9:45:13:531 CEST] 00000176 SystemErr R com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource@e9240c0
[5/10/10 9:45:13:532 CEST] 00000176 SystemErr R class com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource
[5/10/10 9:45:13:539 CEST] 00000176 SystemErr R java.lang.ClassCastException
at java.lang.Throwable.<init>(Throwable.java:181)
at java.lang.Exception.<init>(Exception.java:29)
at java.lang.RuntimeException.<init>(RuntimeException.java:32)
at java.lang.ClassCastException.<init>(ClassCastException.java:29)
at de.ac.action.MAction.execute(MAction.java:77)
at de.ac.web.GetTheView.doService(GetTheView.java:88)
at de.ac.web.GetTheView.doGet(GetTheView.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1096)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:570)
at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3444)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:267)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:815)
at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1466)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:119)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:458)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:387)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:267)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java:556)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java:583)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java:979)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java:1064)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1462)
[5/10/10 9:45:13:539 CEST] 00000176 SystemErr R null
因此,我得到一个 com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource
类的对象,但转换 DataSource
导致 ClassCastException
?
我在另一台服务器上尝试了这个应用程序,WAS 6.1 全新安装,它可以工作。它也适用于 tomcat 环境。
更重要的一件事:它是一个使用 jt400.jar
的 DB2 数据源 - Oracle JDBC 驱动程序和 WAS 6 存在一些类似的问题。
也许你们中的一个人有一些建议?
I've got a webapp running on a Websphere 6.1 Server which JNDI lookups for a datasource. This webapp runs fine, the Websphere server recognizes the datasource ref in the web.xml etc..
Now we added a second webapp which should use this datasource with same configuration but
I recieve a ClassCastException
when I try to access the datasource.
To locate the problem I've put this into my code
try{
InitialContext ctx = new InitialContext();
Object obj = ctx.lookup(N2WebConstants.datasourceJNDI);
System.err.println(obj.toString());
System.err.println(obj.getClass());
con = ((DataSource) obj).getConnection();
}catch (Exception e){
System.err.println(e);
System.err.println(e.getCause());
}
In the error log this is printed
[5/10/10 9:45:13:531 CEST] 00000176 SystemErr R com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource@e9240c0
[5/10/10 9:45:13:532 CEST] 00000176 SystemErr R class com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource
[5/10/10 9:45:13:539 CEST] 00000176 SystemErr R java.lang.ClassCastException
at java.lang.Throwable.<init>(Throwable.java:181)
at java.lang.Exception.<init>(Exception.java:29)
at java.lang.RuntimeException.<init>(RuntimeException.java:32)
at java.lang.ClassCastException.<init>(ClassCastException.java:29)
at de.ac.action.MAction.execute(MAction.java:77)
at de.ac.web.GetTheView.doService(GetTheView.java:88)
at de.ac.web.GetTheView.doGet(GetTheView.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1096)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:570)
at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3444)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:267)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:815)
at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1466)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:119)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:458)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:387)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:267)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java:556)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java:583)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java:979)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java:1064)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1462)
[5/10/10 9:45:13:539 CEST] 00000176 SystemErr R null
So, I get a Object of Class com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource
but casting DataSource
causes the ClassCastException
?
I tried this app on another server, WAS 6.1 fresh install, there it works. It also works in a tomcat env.
One more important thing: It is a DB2 datasource which uses the jt400.jar
- there are some simliar problems with the Oracle JDBC driver and WAS 6.
Maybe one of you has some suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据源可以在几个不同的范围内定义,这会影响其可见性。如果您在运行第一个应用程序的服务器级别定义它,则该服务器之外的任何内容都看不到它。我使用 WebSphere 的“服务器”重载定义,因为大多数商店为每个应用程序创建单独的服务器,以便它们在自己的 JVM 中运行。如果您希望两个应用程序对同一数据源具有可见性,请在两个应用程序共用的更高级别上定义它。
Datasources may be defined at several different scopes which affect their visibility. If you defined it at the level of the server which runs the first app, it won't be visible to anything outside of that server. I'm using WebSphere's overloaded definition of 'server' in the sense that most shops create separate servers for each application so they run in their own JVM. If you want two apps to have visibility to the same datasource, define it at a higher level that is common to both apps.
感谢您更新帖子。可以看到JNDI查找成功了。我还看到 WSJdbcDataSource 中返回的类型实现了 WSDataSource(它又从 javax.sql.DataSource 扩展),所以我在这里没有看到任何错误。
现在你可以在转换它的行之后打印出 con 对象吗? (将是您的 try 块的最后一行)。
您提供的堆栈在 de.ac.action.MAction.execute(MAction.java:77) 处显示为 CCE @。这是你的 try 块的最后一行吗?
最后一条消息为 null(即 e.getCause())
还要对 DataSource 的导入进行健全性检查,以确保它确实是 javax.sql.DataSource
PS:我昨天添加了与注释相同的信息,但不知何故它总是隐藏,您需要单击“显示评论”才能看到。
Thanks for the updated post. I can see that the JNDI look up is successful. I also see that the type returned in WSJdbcDataSource which implments WSDataSource (which in turn extends from javax.sql.DataSource) So i don't see anything wrong here.
Now can you print out the con object after the line that casts it. (would be the last line of your try block).
The stack that you provided shows as CCE @ at de.ac.action.MAction.execute(MAction.java:77). Is this the last line of your try block?
The last message of null (is that the e.getCause())
Also do a sanity check on your import of DataSource to ensure it is indeed javax.sql.DataSource
PS: I added the same info as comment yesterday but somehow it is always hidden and you need to click on show comment to see that.