简体   繁体   中英

How to configure Hibernate 5 SessionFactoryBean for using HikariCP?

Where should I configure connection pool for spring + hibernate application?

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="edu.khai.education.entity"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
        </props>
    </property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
    <property name="driverClassName" value="org.h2.Driver"/>
    <property name="url" value="jdbc:h2:tcp://localhost/~/test"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

I want to use org.hibernate.hikaricp.internal.HikariCPConnectionProvider and I don't have a problem to configure it in Hibernate application, but I don't know how to integrate it with Spring.

Spring uses DataSource bean to create a lot of infrastructure code eg JdbcTemplate or Spring Data JPA @Repository .

Replace the current data source implementation org.apache.commons.dbcp2.BasicDataSource with the com.zaxxer.hikari.HikariDataSource class. Change the current properties to match the HikariCP configuration .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM