class DBQueries {

    public static <K> int bulkRemove(Iterable<K> objectKeys, final String tableName, final Connection connection){
        final String sql = String.format("DELETE FROM cqtbl_%s WHERE objectKey = ?;", tableName);
        PreparedStatement statement = null;
        Boolean previousAutocommit = null;
        int totalRowsModified = 0;
        try{
            previousAutocommit = DBUtils.setAutoCommit(connection, false);
            statement = connection.prepareStatement(sql);
            for(K objectKey: objectKeys) {
                statement.setObject(1, objectKey);
                statement.addBatch();
            }
            int[] rowsModified = statement.executeBatch();
            for (int m : rowsModified) {
                ensureNotNegative(m);
                totalRowsModified += m;
            }
            DBUtils.commit(connection);
            return totalRowsModified;
        }
        catch (NullPointerException e) {
            // Note: here we catch a and rethrow NullPointerException,
            // to allow compatibility with Java Collections Framework,
            // which requires NPE to be thrown for null arguments...
            boolean rolledBack = DBUtils.rollback(connection);
            NullPointerException npe = new NullPointerException("Unable to bulk remove rows containing a null object from the index table: "+ tableName + ". Rolled back: " + rolledBack);
            npe.initCause(e);
            throw npe;
        }
        catch (Exception e){
            boolean rolledBack = DBUtils.rollback(connection);
            throw new IllegalStateException("Unable to remove rows from the index table: " + tableName + ". Rolled back: " + rolledBack, e);
        }finally{
            DBUtils.closeQuietly(statement);
            if (previousAutocommit != null)
                DBUtils.setAutoCommit(connection, previousAutocommit);
        }
    }

}