Class RedisConnectionUtils

java.lang.Object
org.springframework.data.redis.core.RedisConnectionUtils

public abstract class RedisConnectionUtils extends Object
Helper class that provides static methods for obtaining RedisConnection from a RedisConnectionFactory. Includes special support for Spring-managed transactional RedisConnections, e.g. managed by AbstractPlatformTransactionManager..

Used internally by Spring's RedisTemplate. Can also be used directly in application code.

Author:
Costin Leau, Christoph Strobl, Thomas Darimont, Mark Paluch
See Also:
  • Constructor Details

    • RedisConnectionUtils

      public RedisConnectionUtils()
  • Method Details

    • bindConnection

      public static RedisConnection bindConnection(RedisConnectionFactory factory)
      Obtain a RedisConnection from the given RedisConnectionFactory and binds the connection to the current thread to be used in closure-scope, if none is already bound. Considers ongoing transactions by reusing the transaction-bound connection and allows reentrant connection retrieval. Does not bind the connection to potentially ongoing transactions.
      Parameters:
      factory - connection factory
      Returns:
      a new Redis connection without transaction support.
    • bindConnection

      public static RedisConnection bindConnection(RedisConnectionFactory factory, boolean transactionSupport)
      Obtain a RedisConnection from the given RedisConnectionFactory and binds the connection to the current thread to be used in closure-scope, if none is already bound. Considers ongoing transactions by reusing the transaction-bound connection and allows reentrant connection retrieval. Binds also the connection to the ongoing transaction if no connection is already bound if transactionSupport is enabled.
      Parameters:
      factory - connection factory.
      transactionSupport - whether transaction support is enabled.
      Returns:
      a new Redis connection with transaction support if requested.
    • getConnection

      public static RedisConnection getConnection(RedisConnectionFactory factory)
      Obtain a RedisConnection from the given RedisConnectionFactory. Is aware of existing connections bound to the current transaction (when using a transaction manager) or the current thread (when binding a connection to a closure-scope). Does not bind newly created connections to ongoing transactions.
      Parameters:
      factory - connection factory for creating the connection.
      Returns:
      an active Redis connection without transaction management.
    • getConnection

      public static RedisConnection getConnection(RedisConnectionFactory factory, boolean transactionSupport)
      Obtain a RedisConnection from the given RedisConnectionFactory. Is aware of existing connections bound to the current transaction (when using a transaction manager) or the current thread (when binding a connection to a closure-scope).
      Parameters:
      factory - connection factory for creating the connection.
      transactionSupport - whether transaction support is enabled.
      Returns:
      an active Redis connection with transaction management if requested.
    • doGetConnection

      public static RedisConnection doGetConnection(RedisConnectionFactory factory, boolean allowCreate, boolean bind, boolean transactionSupport)
      Actually obtain a RedisConnection from the given RedisConnectionFactory. Is aware of existing connections bound to the current transaction (when using a transaction manager) or the current thread (when binding a connection to a closure-scope). Will create a new RedisConnection otherwise, if allowCreate is true. This method allows for re-entrance as RedisConnectionUtils.RedisConnectionHolder keeps track of ref-count.
      Parameters:
      factory - connection factory for creating the connection.
      allowCreate - whether a new (unbound) connection should be created when no connection can be found for the current thread.
      bind - binds the connection to the thread, in case one was created-
      transactionSupport - whether transaction support is enabled.
      Returns:
      an active Redis connection.
    • releaseConnection

      public static void releaseConnection(@Nullable RedisConnection conn, RedisConnectionFactory factory)
      Closes the given RedisConnection, created via the given factory if not managed externally (i.e. not bound to the transaction).
      Parameters:
      conn - the Redis connection to close.
      factory - the Redis factory that the connection was created with.
    • unbindConnection

      public static void unbindConnection(RedisConnectionFactory factory)
      Unbinds and closes the connection (if any) associated with the given factory from closure-scope. Considers ongoing transactions so transaction-bound connections aren't closed and reentrant closure-scope bound connections. Only the outer-most call to leads to releasing and closing the connection.
      Parameters:
      factory - Redis factory
    • isConnectionTransactional

      public static boolean isConnectionTransactional(RedisConnection connection, RedisConnectionFactory connectionFactory)
      Return whether the given Redis connection is transactional, that is, bound to the current thread by Spring's transaction facilities.
      Parameters:
      connection - Redis connection to check
      connectionFactory - Redis connection factory that the connection was created with
      Returns:
      whether the connection is transactional or not