i want to use Hikari CP in my Spring 4.0.3 context but seems i am missing something.
My bean configuration looks like:
Tested under HikariCP 2.3.8 and Hibernate 4.3.8.Final:
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<constructor-arg>
<props>
<prop key="dataSourceClassName">org.postgresql.ds.PGSimpleDataSource</prop>
<prop key="dataSource.user">${database.username}</prop>
<prop key="dataSource.password">${database.password}</prop>
<prop key="dataSource.databaseName">${database.databaseName}</prop>
<prop key="dataSource.serverName">${database.serverName}</prop>
<prop key="connectionTestQuery">SELECT 1</prop>
</props>
</constructor-arg>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
<constructor-arg ref="hikariConfig" />
</bean>
For the dataSourceClassName, looks at the popular datasource class names table.
ConnectionTestQuery is required for postgresql as per https://github.com/brettwooldridge/HikariCP/issues/225, shouldn't be needed when using a latest jdbc driver version.
For a pure Java-config solution, I've used the following (in a class with a @Configuration
annotation and included in the component-scan-path):
...
@Bean
public DataSource dataSource() {
return new HikariDataSource(hikariConfig());
}
private HikariConfig hikariConfig() {
HikariConfig config = new HikariConfig();
config.setDriverClassName(driverClassName);
config.setJdbcUrl(jdbcUrl);
config.setUsername(username);
config.setPassword(password);
return config;
}
...
HTH
Some of your properties in your example do not need the prefix 'dataSource' if you are using a driver-class.
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<constructor-arg>
<props>
<prop key="driverClassName">${database.driver}</prop>
<prop key="jdbcUrl">${database.database.jdbc.url}</prop>
<prop key="username">${database.user}</prop>
<prop key="password">${database.password}</prop>
</props>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
And port
and databaseName
can be included in the jdbcUrl
.
one way to get the job done is to provide an instance of a DataSource object:
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<property name="dataSource">
<bean class="org.hsqldb.jdbc.JDBCDataSource">
<property name="url" value="${database.database.jdbc.url}"/>
<property name="databaseName" value="${database.name}"/>
<property name="user" value="${database.user}"/>
<property name="password" value="${database.password}"/>
</bean>
</property>
</bean>
</constructor-arg>
</bean>
for sure there are other solutions.
HTH,