I am working on distributed transactions. I am trying to implement XA transaction using Atomikos as the Transaction Manager. One of my issues is being able to get a "XADataSource" from the DB2 database hosted on the Websphere 6.1 server. I am using jndi lookup to get the Datasource as below:
Context ctx=new InitialContext();
XADataSource ds = (XADataSource) ctx.lookup(dataSourceName);
However, this lookup causes the following exception:
[3/21/12 12:08:47:551 EDT] 00000026 SystemErr R java.lang.ClassCastException: com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource incompatible with javax.sql.XADataSource
[3/21/12 12:08:47:551 EDT] 00000026 SystemErr R at com.BusinessEntities.JndiConn.<init>(JndiConn.java:21)
[3/21/12 12:08:47:551 EDT] 00000026 SystemErr R at com.Servlet.InitialServlet.doGet(InitialServlet.java:41)
[3/21/12 12:08:47:551 EDT] 00000026 SystemErr R at com.Servlet.InitialServlet.doPost(InitialServlet.java:186)
[3/21/12 12:08:47:551 EDT] 00000026 SystemErr R at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
[3/21/12 12:08:47:551 EDT] 00000026 SystemErr R at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
[3/21/12 12:08:47:551 EDT] 00000026 SystemErr R at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1101)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:569)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3440)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:267)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:815)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1461)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:118)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:458)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:387)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:102)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:195)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:743)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:873)
[3/21/12 12:08:47:566 EDT] 00000026 SystemErr R at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1473)
The same works fine if attribute "ds" is of type DataSource instead of XADataSource. But I want get XAConnections and hence require XADataSource. I have tried searching a lot but am unable to figure out how to avoid this cast exception. Any pointers on what i should do differently?
You won't be able to cast it to XADataSource.
Both XADataSource and regular DataSource are available to you as a WSJdbcDataSource.
WSJdbcDataSource implements WSDataSource which in turn extends DataSource.
The connection that you have is still XA enabled if the underlying JDBC Driver hosting this datasource is XA enabled.
Sorry I am not able to provide you with ways to getting the XADataSource as that is likely not available.