Redis by default, the transaction support is disabled, must by setting the setEnableTransactionSupport (true) for the use of each of the redistplate explicitly enabled. Doing so forces the current reconnection to be bound to the current thread that triggered multi. If the transaction completes without error, exec is called. Otherwise, Discard is called. Once multiple reconnect queues are entered, a write operation is performed. All read-only operations, such as keys, are piped to new (non-thread-bound) reconnections.
The above content comes from machine translation of the content on the official website.
V Preparations
Before learning this chapter, it is recommended to read the following articles in order to better connect the full text. If you have mastered the following listed knowledge points, please skip:
- Centos install Redis
- SpringBoot(24) integrates Redis
V Transaction configuration
package com.demo.Redis; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * Created by toutou on 2019/2/16. */ @Configuration @EnableTransactionManagement public class RedisTranConfig { @Bean public StringRedisTemplate customStringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); template.setEnableTransactionSupport(true); return template; }}Copy the code
V interface test
@Autowired
StringRedisTemplate template;
@Transactional
@RequestMapping(value = "/testredistran1")
public String testRedisTran1()
{
template.opsForValue().set("test1a", "test1a");
template.opsForValue().set("test1b", "test1b");
template.opsForValue().set("test1c", "test1c");
return "1";
}
@Transactional
@RequestMapping(value = "/testredistran2")
public String testRedisTran2()
{
template.opsForValue().set("test2a", "test2a");
template.opsForValue().set(null, "test2b");
template.opsForValue().set("test2c", "test2c");
return "2";
}
Copy the code
Request to http://localhost:8081/testredistran1 and http://localhost:8081/testredistran2, respectively, can be found test2a has not been written to Redis.
V Other methods
@requestMapping (value = "/testredistran3") public String testredistran3 () {// open transaction template.multi(); template.opsForValue().set("test3a", "test3a"); template.opsForValue().set(null, "test3b"); template.opsForValue().set("test3c", "test3c"); // Close the transaction template.exec(); return "3"; }Copy the code
When a client connects to the Redis service and issues MULTI command, the connection will enter the transaction. After executing MULTI command, all commands will not be executed. It will be placed in a queue, prompting Query, and when the EXEC command is finally executed, Redis will execute the commands in the same order as before.
V Source code address
Github.com/toutouge/ja…
About the author: Focus on basic platform project development. If you have any questions or suggestions, please feel free to comment! Copyright notice: The copyright of this article belongs to the author and the blog garden, welcome to reprint, but without the consent of the author must retain this statement, and give the original text link in a prominent place on the page of the article. For the record: all comments and messages will be answered as soon as possible. You are welcome to correct your mistakes and make progress together. Or direct private message I support the blogger: if you think the article is helpful to you, you can click on the lower right corner of the article [recommendation]. Your encouragement is the author to adhere to the original and continuous writing of the biggest power! \