public PooledDataSource(final int initialSize, final int maxSize, final RecyclingSupplier.Factory<Connection> jdbcConnectionFactory) throws ObjectCreationException { RecyclingSupplierBuilder<Connection> builder = new RecyclingSupplierBuilder<>(maxSize, jdbcConnectionFactory); builder.withInitialSize(initialSize); pool = builder.build(); }
@Override protected LocalObjectPool<T> initialValue() { return new LocalObjectPool<>(globalPool); } };
@Override public Lease<T> get() { return new LeaseImpl<>(rs); } };
@Test(timeout = 20000) public void testPoolUseNoFailures() throws ObjectCreationException, ObjectBorrowException, InterruptedException, TimeoutException, ObjectReturnException, ObjectDisposeException, ExecutionException { RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder(10, new ExpensiveTestObjectFactory(1000000, 1000000, 1, 5)).build(); runTest(pool, 0, 10000); pool.dispose(); }
ScalableObjectPool(final int initialSize, final int maxSize, @Nonnull final RecyclingSupplier.Factory<T> factory, final boolean fair) throws ObjectCreationException { globalPool = new SimpleSmartObjectPool<>(initialSize, maxSize, new ObjectHolderFactory<>(initialSize, factory), fair); localPool = new ThreadLocal<LocalObjectPool<T>>() { @Override protected LocalObjectPool<T> initialValue() { return new LocalObjectPool<>(globalPool); } }; }
public RecyclingSupplier<T> build() throws ObjectCreationException { final ScalableObjectPool<T> underlyingPool = new ScalableObjectPool<>(initialSize, maxSize, factory, fair); final RecyclingSupplier<T> pool; if (borrowHook != null || returnHook != null) { pool = new ObjectPoolWrapper<>(underlyingPool, borrowHook, returnHook); } else { pool = underlyingPool; } if (maintenanceExecutor != null) { maintenanceExecutor.scheduleWithFixedDelay(new MaintenanceRunner<>(underlyingPool, collectBorrowed), maintenanceIntervalMillis, maintenanceIntervalMillis, TimeUnit.MILLISECONDS); } return pool; }
@Override public void recycle(final T object) { recycle(object, null); }
@Override public void doRun() throws Exception { if (collectBorrowed) { underlyingPool.requestReturnFromBorrowersIfNotInUse(); } underlyingPool.scan(new Scanable.ScanHandler<ObjectHolder<T>>() { @Override public boolean handle(final ObjectHolder<T> object) throws ObjectDisposeException { object.validateObjectIfNotBorrowed(); return true; } }); } }
@Override public boolean tryDispose(final long timeoutMillis) throws ObjectDisposeException, InterruptedException { return globalPool.tryDispose(timeoutMillis); }
@Override public void recycle(final T object) { recycle(object, null); }
@Override public boolean scan(final ScanHandler<ObjectHolder<T>> handler) throws Exception { return globalPool.scan(handler); }
@Override public void recycle(final T object) { recycle(object, null); }
public void requestReturnFromBorrowersIfNotInUse() throws InterruptedException { globalPool.requestReturnFromBorrowersIfNotInUse(); }
@Override public ObjectHolder<T> create() throws ObjectCreationException { if (objects.isEmpty()) { return new ObjectHolder<>(factory); } else { return objects.remove(); } }
@Override public boolean validate(final ExpensiveTestObject object, final Exception e) throws IOException { if (e instanceof IOException) { return false; } else { object.testObject(); return true; } }
@Override public void close() throws IOException { doStuff(); }
@Override public ExpensiveTestObject create() { return new ExpensiveTestObject(maxIdleMillis, nrUsesToFailAfter, minOperationMillis, maxOperationMillis); }
@Override public void dispose(final ExpensiveTestObject object) { try { object.close(); } catch (ClosedChannelException ex) { // connection is already closed. } catch (IOException ex) { LOG.warn("Cannot dispose object {}", this, ex); } }
@Test(timeout = 16000) public void testPoolUseNoFailuresStarvation() throws ObjectCreationException, ObjectBorrowException, InterruptedException, TimeoutException, ObjectReturnException, ObjectDisposeException, ExecutionException { RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder(1, new ExpensiveTestObjectFactory(1000000, 1000000, 1, 5)).build(); runTest(pool, 0, 15000); pool.dispose(); }
@Test(timeout = 20000) public void testPoolUseWithMaintenance() throws ObjectCreationException, ObjectBorrowException, InterruptedException, TimeoutException, ObjectReturnException, ObjectDisposeException, ExecutionException { final RecyclingSupplier<ExpensiveTestObject> pool = new RecyclingSupplierBuilder<>(10, new ExpensiveTestObjectFactory()) .withMaintenance(DefaultScheduler.INSTANCE, 10, true).build(); runTest(pool, 5, 20000); try { pool.dispose(); } catch (ObjectDisposeException ex) { Throwables.writeTo(ex, System.err, Throwables.PackageDetail.SHORT); } }