Search in sources :

Example 6 with PalantirSqlException

use of com.palantir.exception.PalantirSqlException in project atlasdb by palantir.

the class OracleOverflowWriteTable method delete.

@Override
public void delete(List<Entry<Cell, Long>> entries) {
    List<Object[]> args = Lists.newArrayListWithCapacity(entries.size());
    for (Map.Entry<Cell, Long> entry : entries) {
        Cell cell = entry.getKey();
        args.add(new Object[] { cell.getRowName(), cell.getColumnName(), entry.getValue() });
    }
    switch(config.overflowMigrationState()) {
        case UNSTARTED:
            deleteOverflow(config.singleOverflowTable(), args);
            break;
        case IN_PROGRESS:
            deleteOverflow(config.singleOverflowTable(), args);
            deleteOverflow(getShortOverflowTableName(), args);
            break;
        // fall through
        case FINISHING:
        case FINISHED:
            deleteOverflow(getShortOverflowTableName(), args);
            break;
        default:
            throw new EnumConstantNotPresentException(OverflowMigrationState.class, config.overflowMigrationState().name());
    }
    String shortTableName = oraclePrefixedTableNames.get(tableRef, conns);
    SqlConnection conn = conns.get();
    try {
        log.info("Got connection for delete on table {}: {}, autocommit={}", shortTableName, conn.getUnderlyingConnection(), conn.getUnderlyingConnection().getAutoCommit());
    } catch (PalantirSqlException | SQLException e) {
    // 
    }
    conn.updateManyUnregisteredQuery(" /* DELETE_ONE (" + shortTableName + ") */ " + " DELETE /*+ INDEX(m " + PrimaryKeyConstraintNames.get(shortTableName) + ") */ " + " FROM " + shortTableName + " m " + " WHERE m.row_name = ? " + "  AND m.col_name = ? " + "  AND m.ts = ?", args);
}
Also used : SQLException(java.sql.SQLException) SqlConnection(com.palantir.nexus.db.sql.SqlConnection) PalantirSqlException(com.palantir.exception.PalantirSqlException) Map(java.util.Map) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Example 7 with PalantirSqlException

use of com.palantir.exception.PalantirSqlException in project atlasdb by palantir.

the class AbstractDbWriteTable method put.

private void put(List<Object[]> args) {
    try {
        String prefixedTableName = prefixedTableNames.get(tableRef, conns);
        conns.get().insertManyUnregisteredQuery("/* INSERT_ONE (" + prefixedTableName + ") */" + " INSERT INTO " + prefixedTableName + " (row_name, col_name, ts, val) " + " VALUES (?, ?, ?, ?) ", args);
    } catch (PalantirSqlException e) {
        if (ExceptionCheck.isUniqueConstraintViolation(e)) {
            throw new KeyAlreadyExistsException("primary key violation", e);
        }
        throw e;
    }
}
Also used : PalantirSqlException(com.palantir.exception.PalantirSqlException) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)

Example 8 with PalantirSqlException

use of com.palantir.exception.PalantirSqlException in project atlasdb by palantir.

the class DbKvs method runWriteForceAutocommit.

private <T> T runWriteForceAutocommit(TableReference tableRef, Function<DbWriteTable, T> runner) {
    ConnectionSupplier conns = new ConnectionSupplier(connections);
    try {
        SqlConnection conn = conns.get();
        boolean autocommit;
        try {
            autocommit = conn.getUnderlyingConnection().getAutoCommit();
        } catch (PalantirSqlException e1) {
            throw Throwables.rewrapAndThrowUncheckedException(e1);
        } catch (SQLException e1) {
            throw Throwables.rewrapAndThrowUncheckedException(e1);
        }
        if (!autocommit) {
            return runWriteFreshConnection(conns, tableRef, runner);
        } else {
            return runner.apply(dbTables.createWrite(tableRef, conns));
        }
    } finally {
        conns.close();
    }
}
Also used : SQLException(java.sql.SQLException) SqlConnection(com.palantir.nexus.db.sql.SqlConnection) PalantirSqlException(com.palantir.exception.PalantirSqlException)

Example 9 with PalantirSqlException

use of com.palantir.exception.PalantirSqlException in project atlasdb by palantir.

the class OracleOverflowWriteTable method putSentinels.

@Override
public void putSentinels(Iterable<Cell> cells) {
    byte[] value = new byte[0];
    long ts = Value.INVALID_VALUE_TIMESTAMP;
    for (List<Cell> batch : Iterables.partition(Ordering.natural().immutableSortedCopy(cells), 1000)) {
        List<Object[]> args = Lists.newArrayListWithCapacity(batch.size());
        for (Cell cell : batch) {
            args.add(new Object[] { cell.getRowName(), cell.getColumnName(), ts, value, null, cell.getRowName(), cell.getColumnName(), ts });
        }
        while (true) {
            try {
                String shortTableName = oraclePrefixedTableNames.get(tableRef, conns);
                conns.get().insertManyUnregisteredQuery("/* INSERT_WHERE_NOT_EXISTS (" + shortTableName + ") */" + " INSERT INTO " + shortTableName + "   (row_name, col_name, ts, val, overflow)" + " SELECT ?, ?, ?, ?, ? FROM DUAL" + " WHERE NOT EXISTS (" + "   SELECT * FROM " + shortTableName + "   WHERE row_name = ?" + "     AND col_name = ?" + "     AND ts = ?)", args);
                break;
            } catch (PalantirSqlException e) {
                // we can't do atomic put if not exists, so retry if we get constraint violations
                if (!ExceptionCheck.isUniqueConstraintViolation(e)) {
                    throw e;
                }
            }
        }
    }
}
Also used : PalantirSqlException(com.palantir.exception.PalantirSqlException) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Example 10 with PalantirSqlException

use of com.palantir.exception.PalantirSqlException in project atlasdb by palantir.

the class BasicSQL method createPreparedStatement.

/**
 * Encapsulates the logic for creating a prepared statement with the arguments set
 */
private PreparedStatement createPreparedStatement(Connection c, String sql, Object[] vs) throws PalantirSqlException {
    PreparedStatement ps;
    ps = Connections.prepareStatement(c, sql);
    List<BlobHandler> toClean = Lists.newArrayList();
    if (vs != null) {
        try {
            for (int i = 0; i < vs.length; i++) {
                BlobHandler cleanup = setObject(c, ps, i + 1, vs[i]);
                if (cleanup != null) {
                    toClean.add(cleanup);
                }
            }
        } catch (Exception e) {
            // if we throw, we need to clean up any blobs we have already made
            for (BlobHandler cleanupBlob : toClean) {
                try {
                    cleanupBlob.freeTemporary();
                } catch (Exception e1) {
                    // $NON-NLS-1$
                    SqlLoggers.LOGGER.error("failed to free temp blob", e1);
                }
            }
            BasicSQLUtils.throwUncheckedIfSQLException(e);
            throw Throwables.throwUncheckedException(e);
        }
    }
    return BlobCleanupPreparedStatement.create(ps, toClean);
}
Also used : BlobHandler(com.palantir.db.oracle.JdbcHandler.BlobHandler) PreparedStatement(java.sql.PreparedStatement) PalantirSqlException(com.palantir.exception.PalantirSqlException) VerboseSQLException(com.palantir.util.sql.VerboseSQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PalantirInterruptedException(com.palantir.exception.PalantirInterruptedException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

PalantirSqlException (com.palantir.exception.PalantirSqlException)11 SQLException (java.sql.SQLException)6 PalantirInterruptedException (com.palantir.exception.PalantirInterruptedException)4 VerboseSQLException (com.palantir.util.sql.VerboseSQLException)4 IOException (java.io.IOException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 PreparedStatement (java.sql.PreparedStatement)4 ExecutionException (java.util.concurrent.ExecutionException)4 Cell (com.palantir.atlasdb.keyvalue.api.Cell)3 BlobHandler (com.palantir.db.oracle.JdbcHandler.BlobHandler)3 SqlTimer (com.palantir.nexus.db.monitoring.timer.SqlTimer)3 KeyAlreadyExistsException (com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)2 FinalSQLString (com.palantir.nexus.db.sql.BasicSQLString.FinalSQLString)2 SqlConnection (com.palantir.nexus.db.sql.SqlConnection)2 Map (java.util.Map)2 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 Throwables (com.palantir.common.base.Throwables)1 NamedThreadFactory (com.palantir.common.concurrent.NamedThreadFactory)1 PTExecutors (com.palantir.common.concurrent.PTExecutors)1