Class SingleConnectionFactory

java.lang.Object
org.springframework.r2dbc.connection.DelegatingConnectionFactory
org.springframework.r2dbc.connection.SingleConnectionFactory
All Implemented Interfaces:
ConnectionFactory, Wrapped<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:
  • Constructor Details

    • SingleConnectionFactory

      public SingleConnectionFactory(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:
    • SingleConnectionFactory

      public SingleConnectionFactory(Connection target, 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 Details

    • 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 Connection> create()
      Specified by:
      create in interface 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<Connection> prepareConnection(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 Connection getCloseSuppressingConnectionProxy(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