Search in sources :

Example 11 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class FindQuery method find.

/**
 * Tries to find a real database entity where the mappings to compare match the given example entity.
 *
 * @param example the example entity to search by
 * @return the matching entity wrapped as optional or an empty optional if no match was found
 */
@SuppressWarnings("unchecked")
@Nonnull
public Optional<E> find(@Nonnull E example) {
    try {
        if (this.type == null) {
            this.type = (Class<E>) example.getClass();
        }
        Watch w = Watch.start();
        PreparedStatement stmt = prepareStmt();
        int i = 1;
        for (Tuple<Operator, Property> filter : getPropertyFilters()) {
            stmt.setObject(i++, filter.getSecond().getValueForDatasource(OMA.class, example));
        }
        try (ResultSet rs = stmt.executeQuery()) {
            if (!rs.next()) {
                return Optional.empty();
            }
            return Optional.of((E) make(rs));
        } finally {
            avarage.addValue(w.elapsedMillis());
        }
    } catch (SQLException e) {
        context.safeClose();
        throw Exceptions.handle().to(OMA.LOG).error(e).withSystemErrorMessage("A database error occurred while executing a FindQuery for %s: %s (%s)", type.getName()).handle();
    } catch (Exception e) {
        throw Exceptions.handle().to(OMA.LOG).error(e).withSystemErrorMessage("An error occurred while executing a FindQuery for %s: %s (%s)", type.getName()).handle();
    }
}
Also used : Operator(sirius.db.jdbc.Operator) OMA(sirius.db.jdbc.OMA) SQLException(java.sql.SQLException) Watch(sirius.kernel.commons.Watch) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Property(sirius.db.mixing.Property) SQLException(java.sql.SQLException) Nonnull(javax.annotation.Nonnull)

Example 12 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class UpdateQuery method update.

/**
 * Updates the given entity in the database by comparing the mappings to compare and updating the mappings to update.
 *
 * @param entity       the entity to update
 * @param invokeChecks determines if before- and after save checks should be performed (<tt>true</tt>)
 *                     or skipped (<tt>false</tt>)
 * @param addBatch     determines if the query should be executed instantly (<tt>false</tt>) or added to the
 *                     batch update (<tt>true</tt>).
 */
@SuppressWarnings("unchecked")
public void update(@Nonnull E entity, boolean invokeChecks, boolean addBatch) {
    try {
        if (this.type == null) {
            this.type = (Class<E>) entity.getClass();
        }
        Watch w = Watch.start();
        if (invokeChecks) {
            getDescriptor().beforeSave(entity);
        }
        PreparedStatement stmt = prepareAndFillForUpdate(entity);
        if (addBatch) {
            addBatch();
        } else {
            stmt.executeUpdate();
            stmt.getConnection().commit();
            avarage.addValue(w.elapsedMillis());
            if (descriptor.isVersioned()) {
                entity.setVersion(entity.getVersion() + 1);
            }
        }
        if (invokeChecks) {
            getDescriptor().afterSave(entity);
        }
    } catch (SQLException e) {
        context.safeClose();
        throw Exceptions.handle().to(OMA.LOG).error(e).withSystemErrorMessage("A database error occurred while executing an UpdateQuery for %s: %s (%s)", type.getName()).handle();
    } catch (Exception e) {
        throw Exceptions.handle().to(OMA.LOG).error(e).withSystemErrorMessage("An error occurred while executing an UpdateQuery for %s: %s (%s)", type.getName()).handle();
    }
}
Also used : SQLException(java.sql.SQLException) Watch(sirius.kernel.commons.Watch) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 13 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class ExternalBatchQuery method tryCommit.

protected void tryCommit(boolean cascade) {
    if (batchBacklog > 0) {
        try {
            Watch w = Watch.start();
            statement.executeBatch();
            avarage.addValues(batchBacklog, w.elapsedMillis());
            batchBacklog = 0;
        } catch (SQLException e) {
            if (cascade) {
                context.safeClose();
            }
            throw Exceptions.handle().to(OMA.LOG).error(e).withSystemErrorMessage("An error occurred while batch executing a statement: %s (%s)").handle();
        }
    }
}
Also used : SQLException(java.sql.SQLException) Watch(sirius.kernel.commons.Watch)

Example 14 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class BaseEntityRefListProperty method cascadeSetNull.

private void cascadeSetNull(TaskContext taskContext, Object idBeingDeleted, BaseEntity<?> other) {
    Watch watch = Watch.start();
    getEntityRefList(accessPath.apply(other)).modify().remove(idBeingDeleted);
    other.getMapper().update(other);
    taskContext.addTiming(NLS.get("BaseEntityRefProperty.cascadedSetNull"), watch.elapsedMillis());
}
Also used : Watch(sirius.kernel.commons.Watch)

Example 15 with Watch

use of sirius.kernel.commons.Watch in project sirius-biz by scireum.

the class BaseAnalyticalTaskScheduler method scheduleBatchesForType.

private void scheduleBatchesForType(Consumer<JSONObject> batchConsumer, Class<? extends B> type) {
    Watch watch = Watch.start();
    if (AnalyticalEngine.LOG.isFINE()) {
        AnalyticalEngine.LOG.FINE("Scheduling batches for type '%s' in '%s'...", type.getSimpleName(), getName());
    }
    scheduleBatches(type, batchConsumer);
    if (AnalyticalEngine.LOG.isFINE()) {
        AnalyticalEngine.LOG.FINE("Scheduling batches for type '%s' in '%s' took: %s", type.getSimpleName(), getName(), watch.duration());
    }
    if (Microtiming.isEnabled()) {
        watch.submitMicroTiming(MICROTIMING_KEY_ANALYTICS, Strings.apply("Scheduled batches for type '%s' in '%s'", type.getSimpleName(), getName()));
    }
}
Also used : Watch(sirius.kernel.commons.Watch)

Aggregations

Watch (sirius.kernel.commons.Watch)53 PreparedStatement (java.sql.PreparedStatement)8 SQLException (java.sql.SQLException)8 Connection (java.sql.Connection)6 HandledException (sirius.kernel.health.HandledException)6 Document (org.bson.Document)5 ResultSet (java.sql.ResultSet)4 TaskContext (sirius.kernel.async.TaskContext)4 ArrayList (java.util.ArrayList)3 OMA (sirius.db.jdbc.OMA)3 Property (sirius.db.mixing.Property)3 BasicDBObject (com.mongodb.BasicDBObject)2 ProcessContext (sirius.biz.process.ProcessContext)2 ObjectStorageSpace (sirius.biz.storage.layer1.ObjectStorageSpace)2 Operator (sirius.db.jdbc.Operator)2 Operation (sirius.kernel.async.Operation)2 Context (sirius.kernel.commons.Context)2 Limit (sirius.kernel.commons.Limit)2 AS400 (com.ibm.as400.access.AS400)1 ProgramCall (com.ibm.as400.access.ProgramCall)1