0

I want to use the apache commons library for BasicDataSource in order to create a connection pool. With apache tomcat 8 server it works fine, however when I tried to make it work with wildfly 8 I get the following exception:

java.lang.ClassCastException: org.jboss.jca.adapters.jdbc.WrapperDataSource cannot be cast to org.apache.commons.dbcp2.BasicDataSource

The way I get the datasource is by doing a lookup in the InitialContext object. This function, returns a datasource class according to the type of server used. In tomcat it returns a BasicDataSource but in WildFly it returns a WrapperDataSource which obviously cannot be cast into a BasicDataSource. I tried changing the DataSource class from the wildfly management but the problem persists and I'm at a loss. Is it impossible to change the type of object the lookup returns? Is there a way to make the WrapperDataSource into a BasicDataSource??

PentaKon
  • 4,139
  • 5
  • 43
  • 80

1 Answers1

0

I'll preface this by saying I've never used this and I'm not sure why you want to use a different connection pooling and datasource provider than Wildfly, but the Wildfly <datasource> does provide a <datasource-class> property that you can conceivably configure to use the BasicDataSource instead.

I did a quick search and found this: https://developer.jboss.org/thread/203514?tstart=0

For JBoss AS 7.1.1, that should conceivably work for Wildfly as well.

<datasource jta="false" jndi-name="java:jboss/datasources/MyDS" pool-name="MyDataSource" enabled="true" use-ccm="false">
    <connection-url>jdbc:mysql://localhost/xxx</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <datasource-class>org.apache.commons.dbcp.BasicDataSource</datasource-class>
    <driver>custom.mysql</driver>
    <security>
        <user-name>xxx</user-name>
        <password>xxx</password>
    </security>
</datasource>

That said, I tried configuring a datasource as specified above (the driver module, in this case for mysql, would has to know about these apache commons JAR resources, my first attempt resulted in a ClassDefNotFound), and then referencing them in a quick test with WildFly 8.1.0 final like this:

InitialContext initCtx = null;
DataSource ds = null;
initCtx = new InitialContext();
ds = (DataSource) initCtx.lookup("java:jboss/datasources/MyDS"); 
Connection conn = ds.getConnection();

This resulted in a 'ds' object that was indeed a BasicDataSource (no class cast exception), but on the next line I got the following exception:

Caused by: java.lang.UnsupportedOperationException: Not supported by BasicDataSource
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1062)
at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:308)
... 45 more

So maybe that helps you get a step further. I would check the Wildfly docs regarding to configure a module for a different jdbc driver and how to configure datasources in general, maybe you don't need to use the commons library at all: https://docs.jboss.org/author/display/WFLY8/DataSource+configuration https://sheemoul.wordpress.com/2014/06/17/configure-mysql-datasource-in-wildfly-8-0/

sprockets
  • 981
  • 1
  • 6
  • 16
  • I know about the `` property and I tried to configure it but from what I understand it needs to be a class from the same `.jar` as the `jdbc driver` used. And since we're using `sqljdbc4.jar` for sql server database I can't use a datasource class from the apache commons library. It throws an exception that `BasicDataSource` was not found in `sqljdbc4.jar`. I don't know how the mysql driver knows about the apache library but the sqljdbc sure doesn't. I'm going to further check the wildfly datasource provider for pooling but my first attempt failed. – PentaKon Jul 07 '15 at 06:50
  • I don't believe it needs to be in the same jar, but you might need to add it to your module definition. That is the same issue I had with my mysql test (it is also not included in the mysql jar): have a look at module descriptors and in particular resource-root: https://docs.jboss.org/author/display/MODULES/Module+descriptors – sprockets Jul 07 '15 at 07:07
  • I added the `.jar` into the resources tag and it didn't work. I also tried adding the package link into the dependencies as a module it still throws an exception. ` ` – PentaKon Jul 07 '15 at 07:29