简体   繁体   English

Springboot中如何配置@Transactional对Redis的支持?

[英]How to configure @Transactional support to Redis in Springboot?

I am currently working on a project which is using Redis mainly for caching purposes, I am using Oracle as the main database and Spring Data JPA to handle the database layer in my project. I am currently working on a project which is using Redis mainly for caching purposes, I am using Oracle as the main database and Spring Data JPA to handle the database layer in my project. I need to know how to use @Transactional annotation support to handle transactions in Redis.我需要知道如何使用@Transactional 注释支持来处理Redis 中的事务。 I have already referred to lots of tutorials and documentation regarding this scenario.我已经参考了很多关于这个场景的教程和文档。 In most of those tutorials, there is always the same set of source codes available.在大多数这些教程中,总是有相同的源代码集可用。 But still, I didn't have a clear idea about the implementation.但是,我仍然对实施没有明确的想法。 Because in my application there is already a data source available which I configured through property file.因为在我的应用程序中已经有一个可用的数据源,我通过属性文件配置了它。 (Oracle database) So I doubt the implementation of the dataSource bean. (Oracle 数据库) 所以我怀疑dataSource bean 的执行。 And I couldn't understand the usage of transactionManager bean too.而且我也无法理解 transactionManager bean 的用法。 How should I implement this properly please give a detailed explanation.我应该如何正确实施,请给出详细解释。

Source code which I found on the internet.我在网上找到的源代码。

@Configuration
@EnableTransactionManagement // 1
public class RedisConfig {

    @Bean
    public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {
        // Configure redisTemplate
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
        / / Open transaction support
        stringRedisTemplate.setEnableTransactionSupport(true); // 2
        return stringRedisTemplate;
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws SQLException {
        return new DataSourceTransactionManager(dataSource()); // 3
    }

    @Bean
    public DataSource dataSource() throws SQLException {
        // ...
    }
}

Updated: Currently configured datasource properties in apppication.properties file.更新:当前在 apppication.properties 文件中配置的数据源属性。

# OracleDB connection settings
spring.datasource.url=jdbc:oracle:thin:@192.168.20.108:1521:orcl
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

# HikariCP settings
spring.datasource.hikari.minimumIdle=5
spring.datasource.hikari.maximumPoolSize=20
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.maxLifetime=2000000
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.poolName=redis-sample-pool

Resource about the Redis Transactions handling 关于 Redis 事务处理的资源

There're two ways to configure DataSource有两种方式来配置 DataSource

  • Using the config file使用配置文件

    spring.datasource.url= jdbc:oracle:thin:@//localhost spring.datasource.username=test spring.datasource.password=test1234 spring.datasource.driver-class-name=oracle.jdbc.OracleDriver #hibernate config dialect for JPA, choose dialect based on the db version spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

You can create manually the same by reading these properties, with the help of DataSourceBuilder您可以在 DataSourceBuilder 的帮助下通过阅读这些属性手动创建相同的

@Bean
public  DataSource dataSource(){
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("oracle.jdbc.OracleDriver");
        dataSourceBuilder.url("jdbc:h2:mem:test");
        dataSourceBuilder.username("test");
        dataSourceBuilder.password("test1234");
        return dataSourceBuilder.build();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM