Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure JNDI datasource in Jboss using HikariCP?

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?

like image 368
Gerson Sosa Avatar asked Aug 12 '14 09:08

Gerson Sosa


People also ask

How can I get JNDI name for DataSource?

To obtain a reference to a data source bound to the JNDI context, look up the data source's JNDI name from the initial context object. The object retrieved in this way is cast as a DataSource type object: ds = (DataSource)ctx. lookup(JndiDataSourceName);

What is HikariCP DataSource?

"HikariCP is solid high-performance JDBC connection pool. A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools may significantly reduce the overall resource usage." - You can find out more here.

What is JNDI name in JBoss?

The JBoss naming service is an implementation of the Java Naming and Directory Interface (JNDI).


1 Answers

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.

like image 199
brettw Avatar answered Sep 30 '22 12:09

brettw