简体   繁体   中英

how to utilize HikariCP with Hibernate?

When I used Hibernate itself, I could've done something like Main.getSession().get(User.class, 1); where getSession() would call openSession() from the session factory. but how can I do the same with HikariDataSource ? Wiki mentioned something about HikariConnectionProvider but no example was given.

@Bean
public DataSource dataSource() throws SQLException {
    if (dbUrl == null || dbUrl.isEmpty()) {
        return new HikariDataSource();
    } else {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(dbUrl);
        return new HikariDataSource(config);
    }
}

If I understand you correctly, you want Hibernate to use connection pool provided by Hikari . If that is the case, then SessionFactory has a method setDataSourc(...)

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    // ...
    return sessionFactory;
}

When you open a session, a connection will be borrowed from Hikari pool.

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