Class RedisConnectionUtils
java.lang.Object
org.springframework.data.redis.core.RedisConnectionUtils
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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Subinterface ofRedisConnection
to be implemented byRedisConnection
proxies. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic RedisConnection
bindConnection
(RedisConnectionFactory factory) Obtain aRedisConnection
from the givenRedisConnectionFactory
and binds the connection to the current thread to be used in closure-scope, if none is already bound.static RedisConnection
bindConnection
(RedisConnectionFactory factory, boolean transactionSupport) Obtain aRedisConnection
from the givenRedisConnectionFactory
and binds the connection to the current thread to be used in closure-scope, if none is already bound.static RedisConnection
doGetConnection
(RedisConnectionFactory factory, boolean allowCreate, boolean bind, boolean transactionSupport) Actually obtain aRedisConnection
from the givenRedisConnectionFactory
.static RedisConnection
getConnection
(RedisConnectionFactory factory) Obtain aRedisConnection
from the givenRedisConnectionFactory
.static RedisConnection
getConnection
(RedisConnectionFactory factory, boolean transactionSupport) Obtain aRedisConnection
from the givenRedisConnectionFactory
.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.static void
releaseConnection
(RedisConnection conn, RedisConnectionFactory factory) Closes the givenRedisConnection
, created via the given factory if not managed externally (i.e. not bound to the transaction).static void
unbindConnection
(RedisConnectionFactory factory) Unbinds and closes the connection (if any) associated with the given factory from closure-scope.
-
Constructor Details
-
RedisConnectionUtils
public RedisConnectionUtils()
-
-
Method Details
-
bindConnection
Obtain aRedisConnection
from the givenRedisConnectionFactory
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 aRedisConnection
from the givenRedisConnectionFactory
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 iftransactionSupport
is enabled.- Parameters:
factory
- connection factory.transactionSupport
- whether transaction support is enabled.- Returns:
- a new Redis connection with transaction support if requested.
-
getConnection
Obtain aRedisConnection
from the givenRedisConnectionFactory
. 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 aRedisConnection
from the givenRedisConnectionFactory
. 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 aRedisConnection
from the givenRedisConnectionFactory
. 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 newRedisConnection
otherwise, ifallowCreate
is true. This method allows for re-entrance asRedisConnectionUtils.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 givenRedisConnection
, 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
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 checkconnectionFactory
- Redis connection factory that the connection was created with- Returns:
- whether the connection is transactional or not
-