如何在Hibernate中使用HikariCP? [英] how to utilize HikariCP with Hibernate?

查看:438
本文介绍了如何在Hibernate中使用HikariCP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我自己使用Hibernate时,我可以做类似Main.getSession().get(User.class, 1);的事情,其中​​getSession()从会话工厂调用openSession().但是我该如何使用HikariDataSource做同样的事情? Wiki提及了有关HikariConnectionProvider的内容,但未给出示例.

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);
    }
}

推荐答案

如果我对您的理解正确,那么您希望Hibernate使用Hikari提供的连接池.如果是这种情况,则SessionFactory具有方法setDataSourc(...)

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;
}

打开会话时,将从Hikari池中借用连接.

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

这篇关于如何在Hibernate中使用HikariCP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆