Hi everyone, I am using Hikari as my connection pool thing (a manager?) with MySQL database. I follo...
m

minucha

over 5 years ago
Hi everyone, I am using Hikari as my connection pool thing (a manager?) with MySQL database. I followed stack overflow and some other tutorials and final code is as follows : I created a
DataSource
class which configures Hikari
public class DataSource {
    private static HikariConfig config = new HikariConfig();
    private static HikariDataSource ds;

    static {
        config.setJdbcUrl("jdbc:<mysql://username>:<mailto:password@q2gen47hi68k1yrb.chr7pe7iynqr.eu-west-1.rds.amazonaws.com|password@q2gen47hi68k1yrb.chr7pe7iynqr.eu-west-1.rds.amazonaws.com>:3306/mmylxkukgpzvjvfx");
        config.setUsername("username");
        config.setPassword("password");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        config.addDataSourceProperty("cachePrepStmts", "true");
        ds = new HikariDataSource(config);
    }

    private DataSource() {
    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}
In all tutorials I followed, they show this code or similer one but they don’t tell where and how to use the
getConnection()
. And the question is when, where and how do I call the
getConnecion()
method?
Am I going to call it in every request I receive (in every api method defined in every single controller) ?
@GetMapping(path = "/all")
    public @ResponseBody Iterable<ExampleEntity> getAllUsers() {
        try {
            Connection connection = DataSource.getConnection();
// what am I gonna do with the connection ?
            return mExampleService.getAll();
        } catch (SQLException ex) {
            System.err.println("Exception Occurred");
        }
    }