Search in sources :

Example 1 with SQLClient

use of io.vertx.ext.sql.SQLClient in project hono by eclipse.

the class SQL method runTransactionally.

/**
 * Run operation transactionally.
 * <p>
 * This function will perform the following operations:
 * <ul>
 *     <li>Open a new connection</li>
 *     <li>Turn off auto-commit mode</li>
 *     <li>Call the provided function</li>
 *     <li>If the provided function failed, perform a <em>Rollback</em> operation</li>
 *     <li>If the provided function succeeded, perform a <em>Commit</em> operation</li>
 *     <li>Close the connection</li>
 * </ul>
 *
 * @param client The client to use.
 * @param tracer The tracer to use.
 * @param function The function to execute while the transaction is open.
 * @param context The span to log to.
 * @param <T> The type of the result.
 * @return A future, tracking the outcome of the operation.
 */
public static <T> Future<T> runTransactionally(final SQLClient client, final Tracer tracer, final SpanContext context, final BiFunction<SQLConnection, SpanContext, Future<T>> function) {
    final Span span = startSqlSpan(tracer, context, "run transactionally", builder -> {
    });
    final Promise<SQLConnection> promise = Promise.promise();
    client.getConnection(promise);
    return promise.future().onSuccess(x -> {
        final Map<String, Object> log = new HashMap<>();
        log.put(Fields.EVENT, "success");
        log.put(Fields.MESSAGE, "connection opened");
        span.log(log);
    }).flatMap(connection -> SQL.setAutoCommit(tracer, span.context(), connection, false).flatMap(y -> function.apply(connection, span.context()).compose(v -> SQL.commit(tracer, span.context(), connection).map(v), x -> SQL.rollback(tracer, span.context(), connection).flatMap(unused -> Future.failedFuture(x)))).onComplete(x -> connection.close())).onComplete(x -> span.finish());
}
Also used : Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) BiFunction(java.util.function.BiFunction) UrlEscapers(com.google.common.net.UrlEscapers) Promise(io.vertx.core.Promise) LoggerFactory(org.slf4j.LoggerFactory) Throwables(com.google.common.base.Throwables) HashMap(java.util.HashMap) SpanBuilder(io.opentracing.Tracer.SpanBuilder) Tags(io.opentracing.tag.Tags) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Consumer(java.util.function.Consumer) SQLException(java.sql.SQLException) List(java.util.List) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) SQLConnection(io.vertx.ext.sql.SQLConnection) Optional(java.util.Optional) Span(io.opentracing.Span) Fields(io.opentracing.log.Fields) URI(java.net.URI) TracingHelper(org.eclipse.hono.tracing.TracingHelper) SQLClient(io.vertx.ext.sql.SQLClient) HashMap(java.util.HashMap) SQLConnection(io.vertx.ext.sql.SQLConnection) Span(io.opentracing.Span)

Aggregations

Throwables (com.google.common.base.Throwables)1 UrlEscapers (com.google.common.net.UrlEscapers)1 Span (io.opentracing.Span)1 SpanContext (io.opentracing.SpanContext)1 Tracer (io.opentracing.Tracer)1 SpanBuilder (io.opentracing.Tracer.SpanBuilder)1 Fields (io.opentracing.log.Fields)1 Tags (io.opentracing.tag.Tags)1 Future (io.vertx.core.Future)1 Promise (io.vertx.core.Promise)1 SQLClient (io.vertx.ext.sql.SQLClient)1 SQLConnection (io.vertx.ext.sql.SQLConnection)1 URI (java.net.URI)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 BiConsumer (java.util.function.BiConsumer)1 BiFunction (java.util.function.BiFunction)1