org.springframework.r2dbc.connection

## Class ConnectionHolder

- java.lang.Object
- - org.springframework.transaction.support.ResourceHolderSupport
  - - org.springframework.r2dbc.connection.ConnectionHolder

- All Implemented Interfaces:
    ResourceHolder

      public class ConnectionHolder
      extends ResourceHolderSupport

  Resource holder wrapping a R2DBC `Connection`. `R2dbcTransactionManager` binds instances of this class to the subscription, for a specific `ConnectionFactory`.

  Inherits rollback-only support for nested R2DBC transactions and reference count functionality from the base class.

  Note: This is an SPI class, not intended to be used by applications.

  Since:
    5.3
  Author:
    Mark Paluch, Christoph Strobl
  See Also:
    `R2dbcTransactionManager`, `ConnectionFactoryUtils`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ConnectionHolder(io.r2dbc.spi.Connection connection)` Create a new ConnectionHolder for the given R2DBC `Connection`, assuming that there is no ongoing transaction.
    `ConnectionHolder(io.r2dbc.spi.Connection connection, boolean transactionActive)` Create a new ConnectionHolder for the given R2DBC `Connection`.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `clear()` Clear the transactional state of this resource holder.
    `io.r2dbc.spi.Connection` | `getConnection()` Return the current `Connection` held by this `ConnectionHolder`.
    `protected boolean` | `hasConnection()` Return whether this holder currently has a `Connection`.
    `protected boolean` | `isTransactionActive()` Return whether this holder represents an active, R2DBC-managed transaction.
    `void` | `released()` Releases the current `Connection`.
    `protected void` | `setConnection(io.r2dbc.spi.Connection connection)` Override the existing Connection with the given `Connection`.
    `protected void` | `setTransactionActive(boolean transactionActive)` Set whether this holder represents an active, R2DBC-managed transaction.

    - ### Methods inherited from class org.springframework.transaction.support.ResourceHolderSupport

      `getDeadline, getTimeToLiveInMillis, getTimeToLiveInSeconds, hasTimeout, isOpen, isRollbackOnly, isSynchronizedWithTransaction, isVoid, requested, reset, resetRollbackOnly, setRollbackOnly, setSynchronizedWithTransaction, setTimeoutInMillis, setTimeoutInSeconds, unbound`

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

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

- - ### Constructor Detail

    - #### ConnectionHolder

          public ConnectionHolder(io.r2dbc.spi.Connection connection)

      Create a new ConnectionHolder for the given R2DBC `Connection`, assuming that there is no ongoing transaction.
      Parameters:
        `connection` - the R2DBC `Connection` to hold
      See Also:
        `ConnectionHolder(Connection, boolean)`

    - #### ConnectionHolder

          public ConnectionHolder(io.r2dbc.spi.Connection connection,
                                  boolean transactionActive)

      Create a new ConnectionHolder for the given R2DBC `Connection`.
      Parameters:
        `connection` - the R2DBC `Connection` to hold
        `transactionActive` - whether the given `Connection` is involved in an ongoing transaction

  - ### Method Detail

    - #### hasConnection

          protected boolean hasConnection()

      Return whether this holder currently has a `Connection`.

    - #### setTransactionActive

          protected void setTransactionActive(boolean transactionActive)

      Set whether this holder represents an active, R2DBC-managed transaction.
      See Also:
        `R2dbcTransactionManager`

    - #### isTransactionActive

          protected boolean isTransactionActive()

      Return whether this holder represents an active, R2DBC-managed transaction.

    - #### setConnection

          protected void setConnection(@Nullable
                                       io.r2dbc.spi.Connection connection)

      Override the existing Connection with the given `Connection`.

      Used for releasing the `Connection` on suspend (with a `null` argument) and setting a fresh `Connection` on resume.

    - #### getConnection

          public io.r2dbc.spi.Connection getConnection()

      Return the current `Connection` held by this `ConnectionHolder`.

      This will be the same `Connection` until `released` gets called on the `ConnectionHolder`, which will reset the held `Connection`, fetching a new `Connection` on demand.

      See Also:
        `released()`

    - #### released

          public void released()

      Releases the current `Connection`.
      Overrides:
        `released` in class `ResourceHolderSupport`

    - #### clear

          public void clear()

      Description copied from class: `ResourceHolderSupport`
      Clear the transactional state of this resource holder.
      Overrides:
        `clear` in class `ResourceHolderSupport`