Code Monkey home page Code Monkey logo

spring-r2dbc-demo's Introduction

Using R2dbc DatabaseClient

Configure Spring r2dbc DatabaseClient

Declare ConnectionFactory similar to DataSource beans of the Jdbc support.

  @Bean
  public ConnectionFactory connectionFactory() {
    return new PostgresqlConnectionFactory(
        PostgresqlConnectionConfiguration.builder()
            .host("localhost")
            .database("demo")
            .username("postgres")
            .password("password")
            .build()
    );
  }

Declare DatabaseClient bean that uses existing ConnectionFactory.

  @Bean
  DatabaseClient databaseClient(ConnectionFactory connectionFactory) {
    return DatabaseClient.builder()
        .connectionFactory(connectionFactory)
        .build();
  }

Define TransactionalOperator bean for programmatic transaction control. This is similar to TransactionTemplate in traditional jdbc.

  @Bean
  ReactiveTransactionManager transactionManager(ConnectionFactory connectionFactory) {
    return new R2dbcTransactionManager(connectionFactory);
  }

  @Bean
  TransactionalOperator transactionalOperator(ReactiveTransactionManager transactionManager) {
    return TransactionalOperator.create(transactionManager);
  }

Spring r2dbc DatabaseClient usage

Use jooq to generate the database query for the execution.

  private String generateInsertQuery() {
    return dslContext.insertInto(DEMO_ENTRY)
            .columns(DEMO_ENTRY.ENTRY)
            .values(UUID.randomUUID().toString())
            .getSQL(INLINED);
  }

Use r2dbc DatabaseClient to execute the generated query.

  private Mono<Integer> createEntry() {
    var insertQuery = generateInsertQuery();

    return databaseClient.sql(insertQuery)
        .fetch()
        .rowsUpdated();
  }

Use TransactionalOperator as the following to make the reactive pipeline a transactional unit.

  public <T> Mono<T> withCreatedEntry(Function<Integer, Mono<T>> action) {
    return createEntry()
        .flatMap(action)
        .as(operator::transactional);
  }

The following test verify an entry is created in db.

  @Test
  void createEntry() {
    var result = demoEntryDao.withCreatedEntry(Mono::just);
    StepVerifier.create(result)
        .expectNext(1)
        .verifyComplete();

    assertEquals(1, entryCount());
  }

When there is an error in the reactive pipeline, transaction should be rollback.

  @Test
  void rollbackEntry() {
    var result = demoEntryDao.withCreatedEntry(__ -> Mono.error(new RuntimeException()));
    StepVerifier.create(result)
            .expectError(RuntimeException.class)
            .verify();

    assertEquals(0, entryCount());
  }

Generate jooq models

The following generate jooq source code with maven.

mvn clean compile -P codegen

spring-r2dbc-demo's People

Contributors

wpnpeiris avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.