@Override public Connection getConnection() throws SQLException { ConnectionAcquisitionProfiler profiler = new ConnectionAcquisitionProfiler(); try { return acquirePooledConnection(profiler); } finally { profiler.logQueryDuration(); } }
@Override public Connection getUnsharedConnection() throws PalantirSqlException { return CloseTracking.wrap(delegate.getConnectionUnchecked()); }
@Override public void close() throws PalantirSqlException { delegate.closeUnchecked(); } }
public static <R, E extends Exception> R wrap(final ResourceType<R, E> type, final R delegate) { if (!log.isErrorEnabled()) { // We leave this check in in case this goes haywire in the field. // This way setting the log level for CloseTracking to OFF can // still disable it entirely. return delegate; } final Tracking tracking = new Tracking(type.name()); R wrapped = type.closeWrapper(delegate, () -> { tracking.close(); type.close(delegate); }); destructorReferences.add(new MyReference(wrapped, tracking)); return wrapped; }
private State normalState() throws SQLException { HikariDataSource dataSourcePool = getDataSourcePool(); boolean keep = false; try { // Setup monitoring HikariPoolMXBean poolProxy = initPoolMbeans(); testDataSource(dataSourcePool); keep = true; return new State(StateType.NORMAL, dataSourcePool, poolProxy, null); } finally { if (!keep) { IOUtils.closeQuietly(dataSourcePool); } } }
@Test public void testConnectionsAreReturnedToPoolWhenClosed() throws SQLException { try (Connection conn1 = manager.getConnection(); Connection conn2 = manager.getConnection()) { try (Connection conn3 = manager.getConnection()) { checkConnection(conn3); } // Try getting a connection again after we returned the last one: should succeed try (Connection conn3 = manager.getConnection()) { checkConnection(conn3); } } }
public static ConnectionManagerAwareDbKvs create(DbKeyValueServiceConfig config) { HikariCPConnectionManager connManager = new HikariCPConnectionManager(config.connection()); ReentrantManagedConnectionSupplier connSupplier = new ReentrantManagedConnectionSupplier(connManager); SqlConnectionSupplier sqlConnSupplier = getSimpleTimedSqlConnectionSupplier(connSupplier); return new ConnectionManagerAwareDbKvs(DbKvs.create(config, sqlConnSupplier), connManager, sqlConnSupplier); }
public synchronized R get() { if (delegate == null) { delegate = open(); } ++refs; final R delegateLocal = delegate; return type.closeWrapper(delegate, new ResourceOnClose<E>() { private boolean closed = false; @Override public synchronized void close() throws E { if (closed) { // arggh return; } closed = true; deref(); } }); }
@Before public void initConnectionManager() { manager = new HikariCPConnectionManager(createConnectionConfig(3)); }
@Override public void close(ConnectionManager r) throws SQLException { r.close(); }
public static Connection wrap(Connection delegate) { return wrap(ResourceTypes.CONNECTION, delegate); }
private static DataSource wrapDataSourceWithVisitor(DataSource ds, final Visitor<Connection> visitor) { return InterceptorDataSource.wrapInterceptor(new InterceptorDataSource(ds) { @Override protected void onAcquire(Connection conn) { visitor.visit(conn); } }); }
@Override public void close() { connectionSupplier.close(); } };
/** * Initializes a connection to the provided database. */ @Override public synchronized void init() throws SQLException { if (state.type == StateType.ZERO) { state = normalState(); } }
private synchronized void deref() throws E { --refs; if (refs > 0) { return; } R delegateLocal = delegate; delegate = null; // close under lock in case underlying is crappy enough to not // handle multiple concurrent co-existing. type.close(delegateLocal); }
@Override public void initUnchecked() { try { init(); } catch (SQLException e) { throw PalantirSqlException.create(e); } } }
@Test public void testCanGetConnection() throws SQLException { try (Connection conn = manager.getConnection()) { checkConnection(conn); } }
public static ConnectionManager wrap(ConnectionManager delegate) { return wrap(ResourceTypes.CONNECTION_MANAGER, delegate); }