How to configure JNDI datasource in Jboss using HikariCP?

Gerson Sosa picture Gerson Sosa · Aug 12, 2014 · Viewed 10.4k times · Source

How to configure a JNDI datasource in jboss configuration file using HikariCP I can't find aything in the help contents of Hikari there is only Tomcat configuration .

I have a Spring webb app, I have a datasource defined inside the application and I want to move this to a JNDI datasource.

My datasource definition is:

<datasource jndi-name="java:jboss/datasources/mydatasource" pool-name="mydatasource" enabled="true" use-java-context="true">
     <connection-url>jdbc:postgresql://localhost:5432/database</connection-url>
     <driver-class>org.postgresql.Driver</driver-class>
     <datasource-class>com.zaxxer.hikari.HikariDataSource</datasource-class>
     <driver>postgresql</driver>
     <pool>
        <min-pool-size>5</min-pool-size>
        <max-pool-size>10</max-pool-size>
     </pool>
     <security>
         <user-name>user</user-name>
         <password>password</password>
     </security>
</datasource>

And the driver definition:

<driver name="postgresql" module="org.postgresql.jdbc">
    <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>

I'm getting this error among others:

ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("add") failed - address: ([ ("subsystem" => "datasources"), ("data-source" => "mydatasource") ]) - failure description: {"JBAS014771: Services with missing/unavailable dependencies" => [ "jboss.driver-demander.java:jboss/datasources/mydatasource is missing [jboss.jdbc-driver.postgresql]", "jboss.data-source.java:jboss/datasources/mydatasource is missing [jboss.jdbc-driver.postgresql]" ]}

So what is the right way to configure this?

EDIT:

Following the guide to create the Tomcat resource and using the information provided in this question I have come to this DataSource definition:

<datasource jta="false" jndi-name="java:jboss/mydatasource" pool-name="mydatasource" enabled="true" use-ccm="false">
    <connection-url>jdbc:postgresql://localhost:5432/databasename</connection-url>
    <driver-class>org.postgresql.Driver</driver-class>
    <driver>postgresql</driver>
    <pool>
        <min-pool-size>5</min-pool-size>
        <max-pool-size>10</max-pool-size>
        <flush-strategy>FailingConnectionOnly</flush-strategy>
    </pool>
    <security>
        <user-name>username</user-name>
        <password>password</password>
    </security>
    <validation>
        <validate-on-match>false</validate-on-match>
        <background-validation>false</background-validation>
    </validation>
    <statement>
         <share-prepared-statements>false</share-prepared-statements>
    </statement>
</datasource>

I installed the postgresql driver in Jboss and declared it.

And in Spring configuration

...
@Bean
public DataSource dataSource() {
    final JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
    dataSourceLookup.setResourceRef(true);
    DataSource dataSourceTemp = null;
    try {
        dataSourceTemp = dataSourceLookup.getDataSource("jdbc/mydatasource");
    } catch (DataSourceLookupFailureException e) {
        log.error("DataSource not found.");
    }
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setDataSource(dataSourceTemp);
    return new HikariDataSource(hikariConfig);
}
...

This code based on HikariJNDIFactory code,everything seems to work but I think I have to create a properties object with the properties of the connection what properties I have to include in the object?

Answer

brettw picture brettw · Aug 12, 2014

Here is the configuration for a "pure" Tomcat JNDI DataSource:

https://github.com/brettwooldridge/HikariCP/wiki/JNDI-DataSource-Factory-(Tomcat,-etc.)

You might also find this answer Re: Spring + Tomcat + JNDI informative:

How to use JNDI DataSource provided by Tomcat in Spring?

Also recommended is the latest HikariCP 2.0.1.