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

这一生的挚爱 提交于 2020-01-11 07:10:19

问题


i want to use Hikari CP in my Spring 4.0.3 context but seems i am missing something.

My bean configuration looks like:

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
   <constructor-arg>
     <bean class="com.zaxxer.hikari.HikariConfig">
       <constructor-arg>
         <props>
           <prop key="dataSource.driverClassName">${database.driver}</prop>
           <prop key="dataSource.jdbcUrl">${database.database.jdbc.url}</prop>
           <prop key="dataSource.port">${database.port}</prop>
           <prop key="dataSource.databaseName">${database.name}</prop>
           <prop key="dataSource.user">${database.user}</prop>
           <prop key="dataSource.password">${database.password}</prop>
         </props>
       </constructor-arg>

     </bean>
   </constructor-arg>
</bean>

but i get an exception:

Caused by: java.lang.IllegalArgumentException: one of either dataSource or dataSourceClassName must be specified
    at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:655)
    at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:66)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 15 more

i have tried to configure dataSourceClassName using the HSQL org.hsqldb.jdbc.JDBCDataSource

that way:

<prop key="dataSourceClassName">org.hsqldb.jdbc.JDBCDataSource</prop>

also that way:

<prop key="dataSource.ClassName">org.hsqldb.jdbc.JDBCDataSource</prop>

both times i got the following exception:

Caused by: java.lang.RuntimeException: java.beans.IntrospectionException: Method not found: setPort
    at com.zaxxer.hikari.util.PropertyBeanSetter.setProperty(PropertyBeanSetter.java:109)
    at com.zaxxer.hikari.util.PropertyBeanSetter.setTargetFromProperties(PropertyBeanSetter.java:61)
    at com.zaxxer.hikari.pool.HikariPool.initializeDataSource(HikariPool.java:497)
    ... 23 more
Caused by: java.beans.IntrospectionException: Method not found: setPort
    at java.beans.PropertyDescriptor.<init>(PropertyDescriptor.java:110)
    at com.zaxxer.hikari.util.PropertyBeanSetter.setProperty(PropertyBeanSetter.java:97)
    ... 25 more

Can someone show me a working Hikari CP Spring 4 bean configuration that works with a HSQL DB?

I am not interested in MySql, PG etc. because i know how to get them working. But i can't manage to get it done with HSQL.

Thanks,

Tech used: Java8, Spring 4.0.3, HSQL 2.3.2


回答1:


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,




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/23369818/how-to-configure-hikari-cp-for-hsql-in-a-spring4-context

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