ru.tinkoff.kora.database.r2dbc.R2dbcConnectionFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of database-r2dbc Show documentation
Show all versions of database-r2dbc Show documentation
Kora database-r2dbc module
package ru.tinkoff.kora.database.r2dbc;
import io.r2dbc.spi.Connection;
import io.r2dbc.spi.Result;
import io.r2dbc.spi.Statement;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import ru.tinkoff.kora.common.Context;
import ru.tinkoff.kora.database.common.QueryContext;
import ru.tinkoff.kora.database.common.telemetry.DataBaseTelemetry;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* Русский: Фабрика соединений R2dbc которая позволяет выполнять запросы в ручном режиме и в рамках транзакции.
*
* English: R2dbc's connection factory that allows you to fulfil requests in manual mode or transaction mode.
*
* @see R2dbcRepository
*/
public interface R2dbcConnectionFactory {
Mono currentConnection();
Mono newConnection();
DataBaseTelemetry telemetry();
Mono inTx(Function> callback);
Mono withConnection(Function> callback);
Flux withConnectionFlux(Function> callback);
default Mono query(QueryContext queryContext, Consumer statementSetter, Function, Mono> resultFluxConsumer) {
return Mono.deferContextual(ctx -> {
var telemetry = this.telemetry().createContext(Context.Reactor.current(ctx), queryContext);
return this.withConnection(connection -> {
var stmt = connection.createStatement(queryContext.sql());
statementSetter.accept(stmt);
return resultFluxConsumer.apply(Flux.from(stmt.execute()));
}).doOnEach(s -> {
if (s.isOnComplete()) {
telemetry.close(null);
} else if (s.isOnError()) {
telemetry.close(s.getThrowable());
}
});
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy