HikariCP connection pool for multiple databases

I am developing a monitoring plugin that queries multiple databases. I would like to use HikariCP to keep the connection open, but I don't know how to create a connection pool.

Does HikariCP only use one pool for multiple databases? or just one pool for one database and my responsibility is to instantiate as many pools as possible that I will be using.

+3


source to share


1 answer


One last thing: a pool is associated with a single database configuration parameter and you are responsible for creating as many pools as possible as a database. Create pools accordingly.

I have DataSourceFactory

to accomplish this:

public final class DataSourceFactory {

    private static final Logger LOG = LoggerFactory.getLogger(DataSourceFactory.class);

    //connection to MySQL
    private static DataSource mySQLDataSource;
    //connection to PostgreSQL
    private static DataSource postgresDataSource;

    private DataSourceFactory() { }

    //generic method to create the DataSource based on configuration
    private static DataSource getDataSource(String configurationProperties) {
        Properties conf = new Properties();
        try {
            conf.load(DataSourceFactory.class.getClassLoader().getResourceAsStream(configurationProperties));
        } catch (IOException e) {
            LOG.error("Can't locate database configuration", e);
        }
        HikariConfig config = new HikariConfig(conf);
        HikariDataSource dataSource = new HikariDataSource(config);
        return dataSource;
    }

    //retrieve the datasource for MySQL
    public static DataSource getMySQLDataSource() {
        LOG.debug("Retrieving data source for MySQL");
        if (mySQLDataSource == null) {
            synchronized(DataSourceFactory.class) {
                if (mySQLDataSource == null) {
                    LOG.debug("Creating data source for MySQL");
                    mySQLDataSource = getDataSource("mysql-connection.properties");
                }
            }
        }
        return mySQLDataSource;
    }

    //retrieve the datasource for Postgres
    public static DataSource getPostgresDataSource() {
        LOG.debug("Retrieving data source for Postgres");
        if (postgresDataSource == null) {
            synchronized(DataSourceFactory.class) {
                if (postgresDataSource == null) {
                    LOG.debug("Creating data source for Postgres");
                    postgresDataSource = getDataSource("postgres-connection.properties");
                }
            }
        }
        return postgresDataSource;
    }
}

      



Here's an example of a file configuration:

dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
dataSource.url=jdbc:mysql://theHostName:thePort/nameOfDatabase
dataSource.user=user
dataSource.password=thIsIsN07mYR3alPa$s
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=100
dataSource.prepStmtCacheSqlLimit=2048
dataSource.useServerPrepStmts=true
autoCommit=false
maximumPoolSize=10

      

+5


source







All Articles