How to set up datasource with Spring for HikariCP?

后端 未结 9 841
傲寒
傲寒 2020-11-27 02:51

Hi I\'m trying to use HikariCP with Spring for connection pool. I\'m using jdbcTempLate and JdbcdaoSupport.
This is my spring configuration file for datasource:

相关标签:
9条回答
  • 2020-11-27 03:45

    May this also can help using configuration file like java class way.

    @Configuration
    @PropertySource("classpath:application.properties")
    public class DataSourceConfig {
        @Autowired
        JdbcConfigProperties jdbc;
    
    
        @Bean(name = "hikariDataSource")
        public DataSource hikariDataSource() {
            HikariConfig config = new HikariConfig();
            HikariDataSource dataSource;
    
            config.setJdbcUrl(jdbc.getUrl());
            config.setUsername(jdbc.getUser());
            config.setPassword(jdbc.getPassword());
            // optional: Property setting depends on database vendor
            config.addDataSourceProperty("cachePrepStmts", "true");
            config.addDataSourceProperty("prepStmtCacheSize", "250");
            config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
            dataSource = new HikariDataSource(config);
    
            return dataSource;
        }
    }
    

    How to use it:

    @Component
    public class Car implements Runnable {
        private static final Logger logger = LoggerFactory.getLogger(AptSommering.class);
    
    
        @Autowired
        @Qualifier("hikariDataSource")
        private DataSource hikariDataSource;
    
    
    }
    
    0 讨论(0)
  • 2020-11-27 03:47

    you need to write this structure on your bean configuration (this is your datasource):

    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="poolName" value="springHikariCP" />
        <property name="connectionTestQuery" value="SELECT 1" />
        <property name="dataSourceClassName" value="${hibernate.dataSourceClassName}" />
        <property name="maximumPoolSize" value="${hibernate.hikari.maximumPoolSize}" />
        <property name="idleTimeout" value="${hibernate.hikari.idleTimeout}" />
    
        <property name="dataSourceProperties">
            <props>
                <prop key="url">${dataSource.url}</prop>
                <prop key="user">${dataSource.username}</prop>
                <prop key="password">${dataSource.password}</prop>
            </props>
        </property>
    </bean>
    
    <!-- HikariCP configuration -->
    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <constructor-arg ref="hikariConfig" />
    </bean>
    

    This is my example and it is working. You just need to put your properties on hibernate.properties and set it before:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:hibernate.properties</value>
            </list>
        </property>
    </bean>
    

    Obs.: the versions are
    log4j: 1.2.16
    springframework: 3.1.4.RELEASE
    HikariCP: 1.4.0

    Properties file (hibernate.properties):

    hibernate.dataSourceClassName=oracle.jdbc.pool.OracleDataSource
    hibernate.hikari.maximumPoolSize=10
    hibernate.hikari.idleTimeout=30000
    dataSource.url=jdbc:oracle:thin:@localhost:1521:xe
    dataSource.username=admin
    dataSource.password=
    
    0 讨论(0)
  • 2020-11-27 03:47

    Using XML configuration, your data source should look something like this:

        <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">  
          <property name="dataSourceProperties" >
            <props>
                <prop key="dataSource.url">jdbc:oracle:thin:@localhost:1521:XE</prop>
                <prop key="dataSource.user">username</prop>
                <prop key="dataSource.password">password</prop>
            </props>
          </property>  
          <property name="dataSourceClassName"   
                    value="oracle.jdbc.driver.OracleDriver" />  
        </bean>  
    
        <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">  
              <constructor-arg ref="hikariConfig" />  
        </bean>  
    

    Or you could skip the HikariConfig bean altogether and use an approach like the one mentioned here

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