views:

266

answers:

2

Hi,

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?

A: 

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.

Kelly French
The datasource has the scope for the server1, which is the only server there. In this server two apps are deployed.
asrijaal
A: 

Hi,

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.

Manglu