@Override public void preparePool() throws Exception { bag = new ConcurrentBag<>(new ConcurrentBag.IBagStateListener() { @Override public Future<Boolean> addBagItem() { return null; } }); for (int i = 0; i < poolSize; i++) { Costs.expendAllocation(); bag.add(new ConcurrentBag.IConcurrentBagEntry() { final AtomicInteger state = new AtomicInteger(ConcurrentBag.IConcurrentBagEntry.STATE_NOT_IN_USE); @Override public boolean compareAndSet(int from, int to) { return state.compareAndSet(from, to); } @Override public int getState() { return state.get(); } }); } }
Pool(SubjectCRIKey key) { this.key = key; this.bag = new ConcurrentBag<>(this::addNewConnection); }
/** * Construct a HikariPool with the specified configuration. * * @param config a HikariConfig instance */ public HikariPool(final HikariConfig config) { this.config = config; this.poolElf = new PoolElf(config); this.dataSource = poolElf.initializeDataSource(); this.poolName = config.getPoolName(); this.connectionBag = new ConcurrentBag<>(this); this.totalConnections = new AtomicInteger(); this.connectionTimeout = config.getConnectionTimeout(); this.lastConnectionFailure = new AtomicReference<>(); this.suspendResumeLock = config.isAllowPoolSuspension() ? new SuspendResumeLock(true) : SuspendResumeLock.FAUX_LOCK; this.addConnectionExecutor = createThreadPoolExecutor(config.getMaximumPoolSize(), "Hikari connection filler (pool " + poolName + ")", config.getThreadFactory(), new ThreadPoolExecutor.DiscardPolicy()); this.closeConnectionExecutor = createThreadPoolExecutor(4, "Hikari connection closer (pool " + poolName + ")", config.getThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); if (config.getScheduledExecutorService() == null) { ThreadFactory threadFactory = config.getThreadFactory() != null ? config.getThreadFactory() : new DefaultThreadFactory("Hikari housekeeper (pool " + poolName + ")", true); this.houseKeepingExecutorService = new ScheduledThreadPoolExecutor(1, threadFactory, new ThreadPoolExecutor.DiscardPolicy()); this.houseKeepingExecutorService.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); this.houseKeepingExecutorService.setRemoveOnCancelPolicy(true); } else { this.houseKeepingExecutorService = config.getScheduledExecutorService(); } this.houseKeepingExecutorService.scheduleAtFixedRate(new HouseKeeper(), HOUSEKEEPING_PERIOD_MS, HOUSEKEEPING_PERIOD_MS, TimeUnit.MILLISECONDS); this.leakTask = new LeakTask(config.getLeakDetectionThreshold(), houseKeepingExecutorService); if (config.getMetricsTrackerFactory() != null) { setMetricsTrackerFactory(config.getMetricsTrackerFactory()); } else { setMetricRegistry(config.getMetricRegistry()); } setHealthCheckRegistry(config.getHealthCheckRegistry()); poolElf.registerMBeans(this); PropertyElf.flushCaches(); initializeConnections(); }
@Override public Object claim() throws Exception { final ConcurrentBag.IConcurrentBagEntry borrow = bag.borrow(1, TimeUnit.DAYS); Costs.expendValidation(); return borrow; }
@Override public void release(Object obj) throws Exception { bag.requite((ConcurrentBag.IConcurrentBagEntry) obj); }