org.springframework.r2dbc.connection

## Class SingleConnectionFactory

- java.lang.Object
- - org.springframework.r2dbc.connection.DelegatingConnectionFactory
  - - org.springframework.r2dbc.connection.SingleConnectionFactory

- All Implemented Interfaces:
    io.r2dbc.spi.ConnectionFactory, io.r2dbc.spi.Wrapped<io.r2dbc.spi.ConnectionFactory>, DisposableBean

      public class SingleConnectionFactory
      extends DelegatingConnectionFactory
      implements DisposableBean

  Implementation of `DelegatingConnectionFactory` that wraps a single R2DBC `Connection` which is not closed after use. Obviously, this is not multi-threading capable.

  Note that at shutdown, someone should close the underlying `Connection` via the `close()` method. Client code will never call close on the `Connection` handle if it is SmartConnectionFactory-aware (e.g. uses `ConnectionFactoryUtils.releaseConnection(Connection, ConnectionFactory)`).

  If client code will call `Connection.close()` in the assumption of a pooled `Connection`, like when using persistence tools, set "suppressClose" to `true`. This will return a close-suppressing proxy instead of the physical Connection.

  This is primarily intended for testing and pipelining usage of connections. For example, it enables easy testing outside an application server for code that expects to work on a `ConnectionFactory`. Note that this implementation does not act as a connection pool-like utility. Connection pooling requires a pooling `ConnectionFactory` such as one from `r2dbc-pool`.

  Since:
    5.3
  Author:
    Mark Paluch
  See Also:
    `create()`, `Connection.close()`, `ConnectionFactoryUtils.releaseConnection(Connection, ConnectionFactory)`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `SingleConnectionFactory(io.r2dbc.spi.Connection target, io.r2dbc.spi.ConnectionFactoryMetadata metadata, boolean suppressClose)` Create a new `SingleConnectionFactory` with a given `Connection` and `ConnectionFactoryMetadata`.
    `SingleConnectionFactory(io.r2dbc.spi.ConnectionFactory targetConnectionFactory)` Constructor for bean-style configuration.
    `SingleConnectionFactory(String url, boolean suppressClose)` Create a new `SingleConnectionFactory` using an R2DBC connection URL.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `reactor.core.publisher.Mono<? extends io.r2dbc.spi.Connection>` | `create()`
    `void` | `destroy()` Close the underlying `Connection`.
    `protected Boolean` | `getAutoCommitValue()` Return whether the returned `Connection`'s "autoCommit" setting should be overridden.
    `protected io.r2dbc.spi.Connection` | `getCloseSuppressingConnectionProxy(io.r2dbc.spi.Connection target)` Wrap the given `Connection` with a proxy that delegates every method call to it but suppresses close calls.
    `protected boolean` | `isSuppressClose()` Return whether the returned `Connection` will be a close-suppressing proxy or the physical `Connection`.
    `protected reactor.core.publisher.Mono<io.r2dbc.spi.Connection>` | `prepareConnection(io.r2dbc.spi.Connection connection)` Prepare the `Connection` before using it.
    `reactor.core.publisher.Mono<Void>` | `resetConnection()` Reset the underlying shared Connection, to be reinitialized on next access.
    `void` | `setAutoCommit(boolean autoCommit)` Set whether the returned `Connection`'s "autoCommit" setting should be overridden.
    `void` | `setSuppressClose(boolean suppressClose)` Set whether the returned `Connection` should be a close-suppressing proxy or the physical `Connection`.

    - ### Methods inherited from class org.springframework.r2dbc.connection.DelegatingConnectionFactory

      `getMetadata, getTargetConnectionFactory, unwrap`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SingleConnectionFactory

          public SingleConnectionFactory(io.r2dbc.spi.ConnectionFactory targetConnectionFactory)

      Constructor for bean-style configuration.

    - #### SingleConnectionFactory

          public SingleConnectionFactory(String url,
                                         boolean suppressClose)

      Create a new `SingleConnectionFactory` using an R2DBC connection URL.
      Parameters:
        `url` - the R2DBC URL to use for accessing `ConnectionFactory` discovery
        `suppressClose` - if the returned `Connection` should be a close-suppressing proxy or the physical `Connection`
      See Also:
        `ConnectionFactories.get(String)`

    - #### SingleConnectionFactory

          public SingleConnectionFactory(io.r2dbc.spi.Connection target,
                                         io.r2dbc.spi.ConnectionFactoryMetadata metadata,
                                         boolean suppressClose)

      Create a new `SingleConnectionFactory` with a given `Connection` and `ConnectionFactoryMetadata`.
      Parameters:
        `target` - underlying target `Connection`
        `metadata` - `ConnectionFactory` metadata to be associated with this `ConnectionFactory`
        `suppressClose` - `true` if the `Connection` should be wrapped with a `Connection` that suppresses `close()` calls (to allow for normal `close()` usage in applications that expect a pooled `Connection`)

  - ### Method Detail

    - #### setSuppressClose

          public void setSuppressClose(boolean suppressClose)

      Set whether the returned `Connection` should be a close-suppressing proxy or the physical `Connection`.

    - #### isSuppressClose

          protected boolean isSuppressClose()

      Return whether the returned `Connection` will be a close-suppressing proxy or the physical `Connection`.

    - #### setAutoCommit

          public void setAutoCommit(boolean autoCommit)

      Set whether the returned `Connection`'s "autoCommit" setting should be overridden.

    - #### getAutoCommitValue

          @Nullable
          protected Boolean getAutoCommitValue()

      Return whether the returned `Connection`'s "autoCommit" setting should be overridden.
      Returns:
        the "autoCommit" value, or `null` if none to be applied

    - #### create

          public reactor.core.publisher.Mono<? extends io.r2dbc.spi.Connection> create()

      Specified by:
        `create` in interface `io.r2dbc.spi.ConnectionFactory`
      Overrides:
        `create` in class `DelegatingConnectionFactory`

    - #### destroy

          public void destroy()

      Close the underlying `Connection`. The provider of this `ConnectionFactory` needs to care for proper shutdown.

      As this bean implements `DisposableBean`, a bean factory will automatically invoke this on destruction of its cached singletons.

      Specified by:
        `destroy` in interface `DisposableBean`

    - #### resetConnection

          public reactor.core.publisher.Mono<Void> resetConnection()

      Reset the underlying shared Connection, to be reinitialized on next access.

    - #### prepareConnection

          protected reactor.core.publisher.Mono<io.r2dbc.spi.Connection> prepareConnection(io.r2dbc.spi.Connection connection)

      Prepare the `Connection` before using it. Applies auto-commit settings if configured.
      Parameters:
        `connection` - the requested `Connection`
      Returns:
        the prepared `Connection`

    - #### getCloseSuppressingConnectionProxy

          protected io.r2dbc.spi.Connection getCloseSuppressingConnectionProxy(io.r2dbc.spi.Connection target)

      Wrap the given `Connection` with a proxy that delegates every method call to it but suppresses close calls.
      Parameters:
        `target` - the original `Connection` to wrap
      Returns:
        the wrapped Connection