How to setup JNDI datasource in Jboss using HikariCP?
How to set up JNDI datasource in jboss config file using HikariCP I can't find anything in Hikari help content, there is only Tomcat config.
I have a Spring web application, I have a data source defined inside the application, and I want to move it to a JNDI data source.
My data source definition:
<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 am getting this error among others:
ERROR [org.jboss.as.controller.management-operation] (controller boot stream) JBAS014613: operation ("add") failed - address: ([("subsystem" => "data sources"), ("data- source "=>" mydatasource ")]) - error 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 how do you set this up correctly?
EDIT:
Following the tutorial on creating a Tomcat resource and using the information provided in this question , I ended up with 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 config
...
@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 is based on the HikariJNDIFactory code, everything works, but I think I need to create a properties object with connection properties, what properties should I include in the object?
source to share
Here is the configuration for a "pure" Tomcat JNDI DataSource:
https://github.com/brettwooldridge/HikariCP/wiki/JNDI-DataSource-Factory-(Tomcat,-etc.)
You can also find this answer Re: Spring + Tomcat + JNDI informative:
How do I use the JNDI DataSource provided by Tomcat in Spring?
It is also recommended to use the latest version of HikariCP 2.0.1.
source to share