Hikari не может создать пул соединений - PullRequest
0 голосов
/ 10 марта 2020

Мне нужно реализовать соединение JDB C Pool с библиотекой Hikari, но я не могу заставить его работать. Мой код следующий:

public class DataSource {

    private static HikariConfig config = new HikariConfig();
    private static HikariDataSource ds;
    static {
        config.setJdbcUrl("jdbc:sqlserver://localhost:1433;database=Test;integratedSecurity=true;");
        config.setUsername("****");
        config.setPassword("*********");
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        ds = new HikariDataSource(config);
        ds.setMaximumPoolSize(100);
        ds.setMinimumIdle(5);
    }
    private DataSource() {
    }

    public static Connection getConnection() throws SQLException {
        HikariPool hikariPool = new HikariPool(config);
        System.out.println("POOL Active Connections -- " + hikariPool.getActiveConnections() + " Total " + hikariPool.getTotalConnections());
        return ds.getConnection();
    }

}
public class MainSample {
    private static final Logger logger = LoggerFactory.getLogger(MainSample.class);

    public static void loadDriver(String driverClassName) throws ClassNotFoundException {
        Class.forName(driverClassName);
    }

    public static void main(String [] args) throws SQLException, ClassNotFoundException {

        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> {
            String threadName = Thread.currentThread().getName();
            System.out.println("POOL " + threadName);
            try {
                fechData();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        });

        ExecutorService executor1 = Executors.newSingleThreadExecutor();
        executor1.submit(() -> {
            String threadName = Thread.currentThread().getName();
            System.out.println("POOL " + threadName);
            try {
                fechData();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        });

        ExecutorService executor2 = Executors.newSingleThreadExecutor();
        executor2.submit(() -> {
            String threadName = Thread.currentThread().getName();
            System.out.println("POOL " + threadName);
            try {
                fechData();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        });
}

Когда я спрашиваю количество активных соединений. Он всегда отвечает 1 активным соединением.

Не могли бы вы дать мне несколько идей о том, как можно решить эту проблему?

...