6

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?

Community
  • 1
  • 1
Gerson Sosa
  • 366
  • 1
  • 4
  • 12

1 Answers1

4

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.

Community
  • 1
  • 1
brettw
  • 10,664
  • 2
  • 42
  • 59
  • 2
    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? – Gerson Sosa Aug 14 '14 at 10:19
  • Does this JBoss example help? http://genesyslab.info/wiki/index.php/Connection_Pooling – brettw Aug 14 '14 at 12:58
  • 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. – Gerson Sosa Aug 15 '14 at 22:39
  • @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 – TecHunter Apr 20 '17 at 12:56
  • @brettw, the link is not up anymore (seems that they changed the target). Could you repost it? – agodinhost Aug 08 '18 at 21:39