From 12d2fbfcbdbe24dc1d3dd9f6c77913bb1ffc3936 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 9 Apr 2018 14:09:58 +0200 Subject: [PATCH] DATAREDIS-601 - Polishing. Extend reference documentation with EnableTransactionManagement. Extend documentation copyright year range. Original pull request: #327. --- src/main/asciidoc/index.adoc | 2 +- .../reference/redis-transactions.adoc | 29 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main/asciidoc/index.adoc b/src/main/asciidoc/index.adoc index 4fd3f8ac0..8bbee0a33 100644 --- a/src/main/asciidoc/index.adoc +++ b/src/main/asciidoc/index.adoc @@ -6,7 +6,7 @@ Costin Leau, Jennifer Hickey, Christoph Strobl, Thomas Darimont, Mark Paluch :toc-placement!: :spring-data-commons-docs: https://raw.githubusercontent.com/spring-projects/spring-data-commons/master/src/main/asciidoc -(C) 2011-2016 The original authors. +(C) 2011-2018 The original authors. NOTE: Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically. diff --git a/src/main/asciidoc/reference/redis-transactions.adoc b/src/main/asciidoc/reference/redis-transactions.adoc index 4c394db75..d2831802b 100644 --- a/src/main/asciidoc/reference/redis-transactions.adoc +++ b/src/main/asciidoc/reference/redis-transactions.adoc @@ -29,36 +29,47 @@ NOTE: An important change has been made to the `exec` methods of `RedisConnectio Transaction Support is disabled by default and has to be explicitly enabled for each `RedisTemplate` in use by setting `setEnableTransactionSupport(true)`. This will force binding the `RedisConnection` in use to the current `Thread` triggering `MULTI`. If the transaction finishes without errors, `EXEC` is called, otherwise `DISCARD`. Once in `MULTI`, `RedisConnection` would queue write operations, all `readonly` operations, such as `KEYS` are piped to a fresh (non thread bound) `RedisConnection`. +.Configuration enabling Transaction Management +==== [source,java] ---- -/** Sample Configuration **/ @Configuration +@EnableTransactionManagement <1> public class RedisTxContextConfiguration { + @Bean public StringRedisTemplate redisTemplate() { StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory()); // explicitly enable transaction support - template.setEnableTransactionSupport(true); + template.setEnableTransactionSupport(true); <2> return template; } @Bean - public PlatformTransactionManager transactionManager() throws SQLException { - return new DataSourceTransactionManager(dataSource()); + public RedisConnectionFactory redisConnectionFactory() { + // jedis || Lettuce || srp || ... } @Bean - public RedisConnectionFactory redisConnectionFactory( // jedis, lettuce, srp,... ); + public PlatformTransactionManager transactionManager() throws SQLException { + return new DataSourceTransactionManager(dataSource()); <3> + } @Bean - public DataSource dataSource() throws SQLException { // ... } + public DataSource dataSource() throws SQLException { + // ... + } } ---- +<1> Configures a Spring Context to enable http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/data-access.html#transaction-declarative[declarative transaction management]. +<2> Configures `RedisTemplate` to participate in transactions by binding connections to the current Thread. +<3> Transaction management requires a `PlatformTransactionManager`. Spring Data Redis does not ship with a `PlatformTransactionManager` implementation. Assuming your application uses JDBC, we can participate in transactions using existing transaction managers. +==== +.Usage Constrainsts +==== [source,java] ---- -/** Usage Constrainsts **/ - // executed on thread bound connection template.opsForValue().set("foo", "bar"); @@ -68,4 +79,4 @@ connection template.keys("*"); // returns null as values set within transaction are not visible template.opsForValue().get("foo"); ---- - +====