我需要使用Hikari库实现JDBC Pool Connection,但无法使其正常工作。我的代码如下:
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个活动连接作为响应。
能否请您给我一些有关如何解决此问题的想法?