1
votes

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?

2
You need to include the stack trace or people will just be guessing. - Brett Kail
In addition to what bkail has mentioned, you also need to provide the topology. Are these two web apps running on the same server. Is this a single server or a ND set up etc. The stack you have provided shows info from your application - at java.lang.ClassCastException.<init>(ClassCastException.java:29) at de.ac.action.ServiceAction.execute(ServiceAction.java:65) We would need to know if the classcast is at the DataSource or some other component/class. This stack does not provide any such information for people to help you. - Manglu
Excuse me but: Sure there is a DataSource expected otherwise my question wouldn't make any sense. One server is defined and on this server two apps should run and share this one datasource. As mentioned in my question: Application one lookups the datasource successfully, application two throws me just this exception. JDBC resource bindings are exactly the same, aswell the JNDI lookupname. - onigunn
Hi, I repeat that you need to provide additional info. You had mentioned this: "NULL Object when I invoke my JNDI lookup " The result of a JNDI lookup should be a reference or a NameNotFoundException. It should never return a null. Also i don't follow how a null can result in a ClassCastException. I can understand a NPE but not a ClassCastException. - Manglu
Hi Manglu, I updated my post, maybe you want to check this out. - onigunn

2 Answers

0
votes

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.

0
votes

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.