private void init() { try (Connection conn = connManager.getConnection()) { createTimestampTable(conn); } catch (SQLException error) { throw PalantirSqlException.create(error); } }
@Override public void close() throws PalantirSqlException { delegate.closeUnchecked(); } }
switch (cm.getDbType()) { case ORACLE: varcharType = "VARCHAR2"; Connection c = cm.getConnection(); try { log.info("The table {} has not been created yet, so we will try to create it.", TABLE_NAME); log.debug("To check whether the table exists we tried to use it. This caused an exception indicating that it did not exist. The exception was: ", e); Connection c = cm.getConnection(); try { c.createStatement().execute("CREATE TABLE " + TABLE_NAME + " (id " + varcharType + "(36) PRIMARY KEY, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");
@Test public void testCantGetConnectionIfClosed() throws SQLException { manager.close(); thrown.expect(SQLException.class); thrown.expectMessage("Hikari connection pool already closed!"); try (Connection conn = manager.getConnection()) { Assert.fail(); } }
@Override public void close(ConnectionManager r) throws SQLException { r.close(); }
@Override public Connection getUnsharedConnection() throws PalantirSqlException { return CloseTracking.wrap(delegate.getConnectionUnchecked()); }
switch (cm.getDbType()) { case ORACLE: case POSTGRESQL:
switch (cm.getDbType()) { case ORACLE: varcharType = "VARCHAR2"; Connection c = cm.getConnection(); try { log.info("The table {} has not been created yet, so we will try to create it.", TABLE_NAME); log.debug("To check whether the table exists we tried to use it. This caused an exception indicating that it did not exist. The exception was: ", e); Connection c = cm.getConnection(); try { c.createStatement().execute("CREATE TABLE " + TABLE_NAME + " (id " + varcharType + "(36) PRIMARY KEY, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");
@After public void closeConnectionManager() throws SQLException { manager.close(); }
@Override public Connection getUnsharedConnection() throws PalantirSqlException { return CloseTracking.wrap(delegate.getConnectionUnchecked()); }
switch (cm.getDbType()) { case ORACLE: case POSTGRESQL:
private static <T> TransactionResult<T> runSimple(ConnectionManager cm, RetriableWriteTransaction<T> tx) { Connection c = null; try { c = cm.getConnection(); c.setAutoCommit(false); T ret = tx.run(c); c.commit(); return TransactionResult.success(ret); } catch (Throwable t) { return TransactionResult.failure(t); } finally { if (c != null) { try { c.close(); } catch (Throwable t) { log.warn("A problem happened trying to close a connection.", t); } } } }
@Override public void close(ConnectionManager r) throws SQLException { r.close(); }
@Override public void close() throws PalantirSqlException { delegate.closeUnchecked(); } }
@Test public void testCantGetConnectionIfPoolExhausted() throws SQLException { try (Connection conn1 = manager.getConnection(); Connection conn2 = manager.getConnection(); Connection conn3 = manager.getConnection()) { thrown.expect(SQLTransientConnectionException.class); thrown.expectMessage("Connection is not available, request timed out after"); try (Connection conn4 = manager.getConnection()) { Assert.fail(); } } }
@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); } } }
@SuppressWarnings("checkstyle:NestedTryDepth") @Test public void testConnectionsAreReturnedToPoolWhenClosedAndOverAllocationsAreStillRejected() throws SQLException { try (Connection conn1 = manager.getConnection(); Connection conn2 = manager.getConnection()) { try (Connection conn3 = manager.getConnection()) { checkConnection(conn3); // Make sure we exhausted the pool boolean caught = false; try (Connection conn4 = manager.getConnection()) { Assert.fail(); } catch (SQLTransientConnectionException e) { caught = true; } Assert.assertTrue(caught); } // Try getting a connection again after we returned the last one: should succeed try (Connection conn3 = manager.getConnection()) { checkConnection(conn3); } } }
@Test public void testCanGetConnection() throws SQLException { try (Connection conn = manager.getConnection()) { checkConnection(conn); } }
private static Callable<Boolean> canCreateKeyValueService() { return () -> { ConnectionManagerAwareDbKvs kvs = null; try { kvs = createKvs(); return kvs.getConnectionManager().getConnection().isValid(5); } catch (Exception ex) { if (ex.getMessage().contains("The connection attempt failed.") || ex.getMessage().contains("the database system is starting up")) { return false; } else { throw ex; } } finally { if (kvs != null) { kvs.close(); } } }; }