How to configure Hikari CP for HSQL in a Spring(4) context?

爱⌒轻易说出口 提交于 2019-12-01 10:59:32

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,

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!