14.7. Spring事务管理器(Spring Transaction Manager)

Redisson为Spring事务(Spring Transaction)提供了基于org.springframework.transaction.PlatformTransactionManager 接口的功能实现。另请查阅事务(Transaction) 章节。

代码范例:

  1. @Configuration
  2. @EnableTransactionManagement
  3. public class RedissonTransactionContextConfig {
  4. @Bean
  5. public TransactionalBean transactionBean() {
  6. return new TransactionalBean();
  7. }
  8. @Bean
  9. public RedissonTransactionManager transactionManager(RedissonClient redisson) {
  10. return new RedissonTransactionManager(redisson);
  11. }
  12. @Bean
  13. public RedissonClient redisson() {
  14. return BaseTest.createInstance();
  15. }
  16. @PreDestroy
  17. public void destroy() {
  18. redisson().shutdown();
  19. }
  20. }
  21. public class TransactionalBean {
  22. @Autowired
  23. private RedissonTransactionManager transactionManager;
  24. @Transactional
  25. public void commitData() {
  26. RTransaction transaction = transactionManager.getCurrentTransaction();
  27. RMap<String, String> map = transaction.getMap("test1");
  28. map.put("1", "2");
  29. }
  30. }