Class Database


  • public final class Database
    extends Object
    Database backed by JDBI with reactive capabilities.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void configureJdbi​(Consumer<org.jdbi.v3.core.Jdbi> jdbiConsumer)
      Allows to access the backing Jdbi instance to enrich its configuration, such as registering mappers.
      static Database create​(org.jdbi.v3.core.Jdbi jdbi)
      Creates a new database backed by the given Jdbi instance.
      <T> reactor.core.publisher.Mono<T> inTransaction​(org.jdbi.v3.core.HandleCallback<T,​?> txCallback)
      Performs a database transaction.
      <E> reactor.core.publisher.Mono<Void> useExtension​(Class<E> extensionType, org.jdbi.v3.core.extension.ExtensionConsumer<E,​?> extensionConsumer)
      Uses a JDBI Extension.
      reactor.core.publisher.Mono<Void> useHandle​(org.jdbi.v3.core.HandleConsumer<?> handleConsumer)
      Acquires a Handle to perform actions on the database.
      reactor.core.publisher.Mono<Void> useTransaction​(org.jdbi.v3.core.HandleConsumer<?> txConsumer)
      Performs a database transaction.
      <T,​E>
      reactor.core.publisher.Mono<T>
      withExtension​(Class<E> extensionType, org.jdbi.v3.core.extension.ExtensionCallback<T,​E,​?> extensionCallback)
      Uses a JDBI Extension.
      <T> reactor.core.publisher.Mono<T> withHandle​(org.jdbi.v3.core.HandleCallback<T,​?> handleCallback)
      Acquires a Handle to perform actions on the database.
    • Method Detail

      • create

        public static Database create​(org.jdbi.v3.core.Jdbi jdbi)
        Creates a new database backed by the given Jdbi instance.

        The given JDBI instance will be enriched as follows:

        • SqlObjectPlugin will be installed
        • Column and argument mappers will ba added for the Snowflake type
        • SerializableTransactionRunner will be set as transaction handler

        It will automatically install the SqlObjectPlugin, and register column mappers and arguments for the Snowflake type. It will also enable support automatic retrying of failed serializable transactions.

        Parameters:
        jdbi - the Jdbi instance backing the database
        Returns:
        a new Database
      • configureJdbi

        public void configureJdbi​(Consumer<org.jdbi.v3.core.Jdbi> jdbiConsumer)
        Allows to access the backing Jdbi instance to enrich its configuration, such as registering mappers. Note that SqlObjectPlugin is already installed by default.

        This method MUST NOT attempt to open any connection to the database. Database operations should be made using the other methods of this Database class.

        Parameters:
        jdbiConsumer - the consumer that mutates the backing Jdbi instance
      • useHandle

        public reactor.core.publisher.Mono<Void> useHandle​(org.jdbi.v3.core.HandleConsumer<?> handleConsumer)
        Acquires a Handle to perform actions on the database. When the consumer returns, the handle is closed and the returned Mono completes. The task is executed on Schedulers.boundedElastic() as database interactions are likely to perform blocking I/O operations.

        This is suited for when the database operations don't need to return a result. If you need to return a result, use withHandle(HandleCallback) instead.

        Parameters:
        handleConsumer - a Consumer using the handle
        Returns:
        a Mono that completes when the consumer returns. If an error is received, the Mono will emit DatabaseException with the underlying JDBI exception as the cause
      • withHandle

        public <T> reactor.core.publisher.Mono<T> withHandle​(org.jdbi.v3.core.HandleCallback<T,​?> handleCallback)
        Acquires a Handle to perform actions on the database. When the callback returns, the handle is closed and the returned Mono completes. The task is executed on Schedulers.boundedElastic() as database interactions are likely to perform blocking I/O operations.

        This is suited for when the database operations need to return a result. If you don't need to return a result, use useHandle(HandleConsumer) instead.

        Type Parameters:
        T - the type of the desired return value
        Parameters:
        handleCallback - a Consumer using the handle
        Returns:
        a Mono that completes when the consumer returns. If an error is received, the Mono will emit DatabaseException with the underlying JDBI exception as the cause
      • useTransaction

        public reactor.core.publisher.Mono<Void> useTransaction​(org.jdbi.v3.core.HandleConsumer<?> txConsumer)
        Performs a database transaction. When the consumer returns, the trasnaction is committed and the returned Mono completes. The task is executed on Schedulers.boundedElastic() as database interactions are likely to perform blocking I/O operations.

        This is suited for when the database operations don't need to return a result. If you need to return a result, use withHandle(HandleCallback) instead.

        Parameters:
        txConsumer - a Consumer using the transaction
        Returns:
        a Mono that completes when the consumer returns. If an error is received, the Mono will emit DatabaseException with the underlying JDBI exception as the cause
      • inTransaction

        public <T> reactor.core.publisher.Mono<T> inTransaction​(org.jdbi.v3.core.HandleCallback<T,​?> txCallback)
        Performs a database transaction. When the callback returns, the transaction is committed and the returned Mono completes. The task is executed on Schedulers.boundedElastic() as database interactions are likely to perform blocking I/O operations.

        This is suited for when the database operations need to return a result. If you don't need to return a result, use useHandle(HandleConsumer) instead.

        Type Parameters:
        T - the type of the desired return value
        Parameters:
        txCallback - a Consumer using the transaction
        Returns:
        a Mono that completes when the consumer returns. If an error is received, the Mono will emit DatabaseException with the underlying JDBI exception as the cause
      • useExtension

        public <E> reactor.core.publisher.Mono<Void> useExtension​(Class<E> extensionType,
                                                                  org.jdbi.v3.core.extension.ExtensionConsumer<E,​?> extensionConsumer)
        Uses a JDBI Extension. When the consumer returns, any acquired connections are closed and the returned Mono completes. The task is executed on Schedulers.boundedElastic() as database interactions are likely to perform blocking I/O operations.

        This is suited for when the use of the extension doesn't need to return a result. If you need to return a result, use withExtension(Class, ExtensionCallback) instead.

        Type Parameters:
        E - the extension type
        Parameters:
        extensionType - the type of extension to use
        extensionConsumer - the consumer that uses the extension
        Returns:
        a Mono that completes when the consumer returns. If an error is received, the Mono will emit DatabaseException with the underlying JDBI exception as the cause
      • withExtension

        public <T,​E> reactor.core.publisher.Mono<T> withExtension​(Class<E> extensionType,
                                                                        org.jdbi.v3.core.extension.ExtensionCallback<T,​E,​?> extensionCallback)
        Uses a JDBI Extension. When the callback returns, any acquired connections are closed and the returned Mono completes. The task is executed on Schedulers.boundedElastic() as database interactions are likely to perform blocking I/O operations.

        This is suited for when the use of the extension needs to return a result. If you don't need to return a result, use useExtension(Class, ExtensionConsumer) instead.

        Type Parameters:
        T - the return value type
        E - the extension type
        Parameters:
        extensionType - the type of extension to use
        extensionCallback - the callback that uses the extension
        Returns:
        a Mono that completes when the callback returns. If an error is received, the Mono will emit DatabaseException with the underlying JDBI exception as the cause