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();
}
}
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();
}
}
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();
}
}
}
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());
}
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()));
}
}
Aggregations