How to configure JNDI datasource in Jboss using HikariCP?

12,093

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.

Share:
12,093
Gerson Sosa
Author by

Gerson Sosa

Software Engineer graduted from the Universidad Distrital Francisco José de Caldas from Colombia. I have a long relationship with JVM languages mostly Java and Kotlin, but I’ve also explored the Go and JS-Typescript world so overall a curious passionate developer.

Updated on September 16, 2022

Comments

  • Gerson Sosa
    Gerson Sosa over 1 year

    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?

  • Gerson Sosa
    Gerson Sosa almost 10 years
    Thanks for your quick answer, I have used what you suggested and I have some problems still: In the Tomcat resource declaration there's a property "factory" that I can't find in Jboss DataSource definition is that property mandatory to construct the DataSource? I can declare a Datasource in Jboss and retrieve it to the Spring configuration class but the problem is how do I declare the datasource with the class HikariDataSource?
  • brettw
    brettw almost 10 years
  • Gerson Sosa
    Gerson Sosa almost 10 years
    Thanks the links helped me a lot, but I took a sightly different approach, I create the HikariDataSource Object based on the code on HikariJNDIFactory, I loaded the JNDI DataSource with a Spring Class and put the datasource to a new HikariConfig Object but I still don't know how to build the Properties object for the HikariConfig Constructor.
  • TecHunter
    TecHunter about 7 years
    @GersonSosa same issue here. I can create a HikariConfig, the jndi datasource and the entitymanager but cant find how to put them all together. In HikariCPConnectionProvider there is a DataSource property but cannot be accessed with spring. when we instanciate the provider it only accepts String from jpaProperties map
  • agodinhost
    agodinhost almost 6 years
    @brettw, the link is not up anymore (seems that they changed the target). Could you repost it?