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

前端 未结 4 1996
南笙
南笙 2021-01-15 06:22

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

My bean configuration looks like:



        
相关标签:
4条回答
  • 2021-01-15 07:04

    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.

    0 讨论(0)
  • 2021-01-15 07:14

    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

    0 讨论(0)
  • 2021-01-15 07:18

    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.

    0 讨论(0)
  • 2021-01-15 07:22

    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,

    0 讨论(0)
提交回复
热议问题