0

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);
    }
}
user1865027
  • 3,505
  • 6
  • 33
  • 71

1 Answers1

0

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.

ThanhLoyal
  • 393
  • 3
  • 11