Search in sources :

Example 1 with DataAccessException

use of org.jooq.exception.DataAccessException in project jOOQ by jOOQ.

the class MySQLDatabase method getRoutines0.

@Override
protected List<RoutineDefinition> getRoutines0() throws SQLException {
    List<RoutineDefinition> result = new ArrayList<RoutineDefinition>();
    try {
        create(true).fetchCount(PROC);
    } catch (DataAccessException e) {
        log.warn("Table unavailable", "The `mysql`.`proc` table is unavailable. Stored procedures cannot be loaded. Check if you have sufficient grants");
        return result;
    }
    Result<Record6<String, String, String, byte[], byte[], ProcType>> records = create().select(Proc.DB, Proc.NAME, Proc.COMMENT, Proc.PARAM_LIST, Proc.RETURNS, Proc.TYPE).from(PROC).where(DB.in(getInputSchemata())).orderBy(DB, Proc.NAME).fetch();
    Map<Record, Result<Record6<String, String, String, byte[], byte[], ProcType>>> groups = records.intoGroups(new Field[] { Proc.DB, Proc.NAME });
    // procedures and functions with the same signature.
    for (Entry<Record, Result<Record6<String, String, String, byte[], byte[], ProcType>>> entry : groups.entrySet()) {
        Result<?> overloads = entry.getValue();
        for (int i = 0; i < overloads.size(); i++) {
            Record record = overloads.get(i);
            SchemaDefinition schema = getSchema(record.get(DB));
            String name = record.get(Proc.NAME);
            String comment = record.get(Proc.COMMENT);
            String params = new String(record.get(Proc.PARAM_LIST));
            String returns = new String(record.get(Proc.RETURNS));
            ProcType type = record.get(Proc.TYPE);
            if (overloads.size() > 1) {
                result.add(new MySQLRoutineDefinition(schema, name, comment, params, returns, type, "_" + type.name()));
            } else {
                result.add(new MySQLRoutineDefinition(schema, name, comment, params, returns, type, null));
            }
        }
    }
    return result;
}
Also used : RoutineDefinition(org.jooq.util.RoutineDefinition) SchemaDefinition(org.jooq.util.SchemaDefinition) ArrayList(java.util.ArrayList) Result(org.jooq.Result) ProcType(org.jooq.util.mysql.mysql.enums.ProcType) Record6(org.jooq.Record6) Record(org.jooq.Record) DataAccessException(org.jooq.exception.DataAccessException)

Example 2 with DataAccessException

use of org.jooq.exception.DataAccessException in project jOOQ by jOOQ.

the class LoaderImpl method executeSQL.

private void executeSQL(Iterator<? extends Object[]> iterator) throws SQLException {
    Object[] row = null;
    BatchBindStep bind = null;
    InsertQuery<R> insert = null;
    execution: {
        rows: while (iterator.hasNext() && ((row = iterator.next()) != null)) {
            try {
                // [#5858] Work with non String[] types from here on (e.g. after CSV import)
                if (row.getClass() != Object[].class)
                    row = Arrays.copyOf(row, row.length, Object[].class);
                //         in case LoaderFieldMapper was used.
                if (fields == null)
                    fields0(row);
                // [#2741]         TODO: This logic will be externalised in new SPI
                for (int i = 0; i < row.length; i++) if (StringUtils.equals(nullString, row[i]))
                    row[i] = null;
                else if (i < fields.length && fields[i] != null)
                    if (fields[i].getType() == byte[].class && row[i] instanceof String)
                        row[i] = DatatypeConverter.parseBase64Binary((String) row[i]);
                // TODO: In batch mode, we can probably optimise this by not creating
                // new statements every time, just to convert bind values to their
                // appropriate target types. But beware of SQL dialects that tend to
                // need very explicit casting of bind values (e.g. Firebird)
                processed++;
                // in some dialects
                if (onDuplicate == ON_DUPLICATE_KEY_IGNORE) {
                    SelectQuery<R> select = create.selectQuery(table);
                    for (int i = 0; i < row.length; i++) if (i < fields.length && primaryKey[i])
                        select.addConditions(getCondition(fields[i], row[i]));
                    try {
                        if (create.fetchExists(select)) {
                            ignored++;
                            continue rows;
                        }
                    } catch (DataAccessException e) {
                        errors.add(new LoaderErrorImpl(e, row, processed - 1, select));
                    }
                }
                buffered++;
                if (insert == null)
                    insert = create.insertQuery(table);
                for (int i = 0; i < row.length; i++) if (i < fields.length && fields[i] != null)
                    addValue0(insert, fields[i], row[i]);
                // dialects execute a SELECT and then either an INSERT or UPDATE
                if (onDuplicate == ON_DUPLICATE_KEY_UPDATE) {
                    insert.onDuplicateKeyUpdate(true);
                    for (int i = 0; i < row.length; i++) if (i < fields.length && fields[i] != null && !primaryKey[i])
                        addValueForUpdate0(insert, fields[i], row[i]);
                } else // Don't do anything. Let the execution fail
                if (onDuplicate == ON_DUPLICATE_KEY_ERROR) {
                }
                try {
                    if (bulk != BULK_NONE) {
                        if (bulk == BULK_ALL || processed % bulkAfter != 0) {
                            insert.newRecord();
                            continue rows;
                        }
                    }
                    if (batch != BATCH_NONE) {
                        if (bind == null)
                            bind = create.batch(insert);
                        bind.bind(insert.getBindValues().toArray());
                        insert = null;
                        if (batch == BATCH_ALL || processed % (bulkAfter * batchAfter) != 0)
                            continue rows;
                    }
                    if (bind != null)
                        bind.execute();
                    else if (insert != null)
                        insert.execute();
                    stored += buffered;
                    executed++;
                    buffered = 0;
                    bind = null;
                    insert = null;
                    if (commit == COMMIT_AFTER)
                        if ((processed % batchAfter == 0) && ((processed / batchAfter) % commitAfter == 0))
                            commit();
                } catch (DataAccessException e) {
                    errors.add(new LoaderErrorImpl(e, row, processed - 1, insert));
                    ignored += buffered;
                    buffered = 0;
                    if (onError == ON_ERROR_ABORT)
                        break execution;
                }
            } finally {
                if (listener != null)
                    listener.row(result);
            }
        // rows:
        }
        // Execute remaining batch
        if (buffered != 0) {
            try {
                if (bind != null)
                    bind.execute();
                if (insert != null)
                    insert.execute();
                stored += buffered;
                executed++;
                buffered = 0;
            } catch (DataAccessException e) {
                errors.add(new LoaderErrorImpl(e, row, processed - 1, insert));
                ignored += buffered;
                buffered = 0;
            }
            if (onError == ON_ERROR_ABORT)
                break execution;
        }
    // execution:
    }
    // Rollback on errors in COMMIT_ALL mode
    try {
        if (commit == COMMIT_ALL) {
            if (!errors.isEmpty()) {
                stored = 0;
                rollback();
            } else {
                commit();
            }
        } else // Commit remaining elements in COMMIT_AFTER mode
        if (commit == COMMIT_AFTER) {
            commit();
        }
    } catch (DataAccessException e) {
        errors.add(new LoaderErrorImpl(e, null, processed - 1, null));
    }
}
Also used : SelectQuery(org.jooq.SelectQuery) BatchBindStep(org.jooq.BatchBindStep) DataAccessException(org.jooq.exception.DataAccessException)

Example 3 with DataAccessException

use of org.jooq.exception.DataAccessException in project jOOQ by jOOQ.

the class DefaultConnectionProvider method setHoldability.

/**
     * Convenience method to access {@link Connection#setHoldability(int)}.
     */
public final void setHoldability(int holdability) throws DataAccessException {
    try {
        log.debug("setting holdability", holdability);
        connection.setHoldability(holdability);
    } catch (Exception e) {
        throw new DataAccessException("Cannot set holdability", e);
    }
}
Also used : DataAccessException(org.jooq.exception.DataAccessException) SQLException(java.sql.SQLException) DataAccessException(org.jooq.exception.DataAccessException)

Example 4 with DataAccessException

use of org.jooq.exception.DataAccessException in project jOOQ by jOOQ.

the class DefaultConnectionProvider method commit.

/**
     * Convenience method to access {@link Connection#commit()}.
     */
public final void commit() throws DataAccessException {
    try {
        log.debug("commit");
        connection.commit();
    } catch (Exception e) {
        throw new DataAccessException("Cannot commit transaction", e);
    }
}
Also used : DataAccessException(org.jooq.exception.DataAccessException) SQLException(java.sql.SQLException) DataAccessException(org.jooq.exception.DataAccessException)

Example 5 with DataAccessException

use of org.jooq.exception.DataAccessException in project jOOQ by jOOQ.

the class DefaultConnectionProvider method rollback.

/**
     * Convenience method to access {@link Connection#rollback()}.
     */
public final void rollback() throws DataAccessException {
    try {
        log.debug("rollback");
        connection.rollback();
    } catch (Exception e) {
        throw new DataAccessException("Cannot rollback transaction", e);
    }
}
Also used : DataAccessException(org.jooq.exception.DataAccessException) SQLException(java.sql.SQLException) DataAccessException(org.jooq.exception.DataAccessException)

Aggregations

DataAccessException (org.jooq.exception.DataAccessException)34 SQLException (java.sql.SQLException)14 Test (org.junit.Test)5 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)4 Timed (com.codahale.metrics.annotation.Timed)4 Connection (java.sql.Connection)4 Consumes (javax.ws.rs.Consumes)4 POST (javax.ws.rs.POST)4 Event (keywhiz.log.Event)4 SecretController (keywhiz.service.daos.SecretController)4 ConflictException (keywhiz.service.exceptions.ConflictException)4 IOException (java.io.IOException)3 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)3 HashMap (java.util.HashMap)3 Response (javax.ws.rs.core.Response)3 SanitizedSecret (keywhiz.api.model.SanitizedSecret)3 Secret (keywhiz.api.model.Secret)3 StringReader (java.io.StringReader)2 URI (java.net.URI)2 Savepoint (java.sql.Savepoint)2