use of com.palantir.nexus.db.sql.SqlConnection in project atlasdb by palantir.
the class DbKvs method runWriteFreshConnection.
/**
* Runs with a new connection, in a new thread so we don't reuse the connection we're
* getting from ReentrantManagedConnectionSupplier.
* Note that most of DbKvs reuses connections so unlike most other calls,
* this can block on getting a new connection if the pool is full.
* To avoid deadlocks or long pauses, use this only when necessary.
*/
private <T> T runWriteFreshConnection(ConnectionSupplier conns, TableReference tableRef, Function<DbWriteTable, T> runner) {
log.debug("Running in a new thread to turn autocommit on for write");
AtomicReference<T> result = Atomics.newReference();
Thread writeThread = new Thread(() -> {
SqlConnection freshConn = conns.getFresh();
try {
result.set(runner.apply(dbTables.createWrite(tableRef, new ConnectionSupplier(Suppliers.ofInstance(freshConn)))));
} finally {
try {
Connection conn = freshConn.getUnderlyingConnection();
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
log.error("Failed to close db connection performing write with fresh connection.", e);
}
}
});
writeThread.start();
try {
writeThread.join();
} catch (InterruptedException e) {
throw Throwables.rewrapAndThrowUncheckedException(e);
}
return result.get();
}
use of com.palantir.nexus.db.sql.SqlConnection in project atlasdb by palantir.
the class PostgresDdlTableTest method shouldCompactIfVacuumWasNeverPerformedAndTheDbTimeIsLessThanCompactInterval.
@Test
public void shouldCompactIfVacuumWasNeverPerformedAndTheDbTimeIsLessThanCompactInterval() throws Exception {
SqlConnection sqlConnection = setUpSqlConnection(null, COMPACT_INTERVAL_MILLIS / SMALL_POSITIVE_FACTOR);
assertThatVacuumWasPerformed(sqlConnection);
}
use of com.palantir.nexus.db.sql.SqlConnection in project atlasdb by palantir.
the class PostgresDdlTableTest method shouldCompactIfVacuumWasNeverPerformedAndTheDbTimeIsMoreThanCompactInterval.
@Test
public void shouldCompactIfVacuumWasNeverPerformedAndTheDbTimeIsMoreThanCompactInterval() throws Exception {
SqlConnection sqlConnection = setUpSqlConnection(null, COMPACT_INTERVAL_MILLIS * SMALL_POSITIVE_FACTOR);
assertThatVacuumWasPerformed(sqlConnection);
}
use of com.palantir.nexus.db.sql.SqlConnection in project atlasdb by palantir.
the class PostgresDdlTableTest method shouldCompactIfVacuumWasPerformedExactlyBeforeCompactInterval.
@Test
public void shouldCompactIfVacuumWasPerformedExactlyBeforeCompactInterval() throws Exception {
SqlConnection sqlConnection = setUpSqlConnection(NOW_MILLIS - COMPACT_INTERVAL_MILLIS, NOW_MILLIS);
assertThatVacuumWasPerformed(sqlConnection);
}
use of com.palantir.nexus.db.sql.SqlConnection in project atlasdb by palantir.
the class PostgresDdlTableTest method shouldCompactIfVacuumTimestampExceedsNowTimestampByMoreThanCompactInterval.
@Test
public void shouldCompactIfVacuumTimestampExceedsNowTimestampByMoreThanCompactInterval() {
SqlConnection sqlConnection = setUpSqlConnection(NOW_MILLIS + COMPACT_INTERVAL_MILLIS * SMALL_POSITIVE_FACTOR, NOW_MILLIS);
assertThatVacuumWasNotPerformed(sqlConnection);
}
Aggregations