Search in sources :

Example 1 with SqlConnection

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();
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SqlConnection(com.palantir.nexus.db.sql.SqlConnection) SqlConnection(com.palantir.nexus.db.sql.SqlConnection)

Example 2 with SqlConnection

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);
}
Also used : SqlConnection(com.palantir.nexus.db.sql.SqlConnection) Test(org.junit.Test)

Example 3 with 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);
}
Also used : SqlConnection(com.palantir.nexus.db.sql.SqlConnection) Test(org.junit.Test)

Example 4 with 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);
}
Also used : SqlConnection(com.palantir.nexus.db.sql.SqlConnection) Test(org.junit.Test)

Example 5 with 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);
}
Also used : SqlConnection(com.palantir.nexus.db.sql.SqlConnection) Test(org.junit.Test)

Aggregations

SqlConnection (com.palantir.nexus.db.sql.SqlConnection)23 Test (org.junit.Test)11 AgnosticResultSet (com.palantir.nexus.db.sql.AgnosticResultSet)6 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 Before (org.junit.Before)3 ConnectionSupplier (com.palantir.atlasdb.keyvalue.dbkvs.impl.ConnectionSupplier)2 OverflowSequenceSupplier (com.palantir.atlasdb.keyvalue.dbkvs.impl.oracle.OverflowSequenceSupplier)2 PalantirSqlException (com.palantir.exception.PalantirSqlException)2 AgnosticResultRow (com.palantir.nexus.db.sql.AgnosticResultRow)2 ExecutionException (java.util.concurrent.ExecutionException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Cell (com.palantir.atlasdb.keyvalue.api.Cell)1 TableMappingNotFoundException (com.palantir.atlasdb.keyvalue.impl.TableMappingNotFoundException)1 SqlTimer (com.palantir.nexus.db.monitoring.timer.SqlTimer)1 AgnosticResultSetImpl (com.palantir.nexus.db.sql.AgnosticResultSetImpl)1 ConnectionBackedSqlConnectionImpl (com.palantir.nexus.db.sql.ConnectionBackedSqlConnectionImpl)1 SQL (com.palantir.nexus.db.sql.SQL)1 SqlConnectionHelper (com.palantir.nexus.db.sql.SqlConnectionHelper)1 ArrayList (java.util.ArrayList)1