shardingsphere-jdbc 5.0.0 LOCAL Transaction
Spring framework 5.2.12
mysql 默认隔离级别 REPEATABLE-READ
@Transactional(rollbackFor = Exception.class,isolation = Isolation.READ_UNCOMMITTED)
我打断点 最后发现 spring 在处理这个判断的时候 因为 ShardingSphereConnection 默认的隔离级别预制了一个 TRANSACTION_READ_UNCOMMITTED
if (definition != null && definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {}
导致这行代码为 false
有没有人遇见过,这个咋解决~
1
dust0522 OP if (currentIsolation != definition.getIsolationLevel()) 应该是这个代码是 false 贴错了
|
2
dust0522 OP git 提了 issues 没人理 https://github.com/apache/shardingsphere/issues/24188
|
3
WashFreshFresh 2023-02-21 13:51:34 +08:00
shardingjdbc 很多公司都是自己 fork 了自己玩自己的...
|
4
dust0522 OP @WashFreshFresh 那我 fork 下来把 ShardingSphereConnection 默认的隔离级别预制成 REPEATABLE-READ 试试
|
5
OldCarMan 2023-02-21 23:03:51 +08:00
可以不可以换种思路,通过 transactionManager 来设定你的事务隔离级别,大致代码如下:
1. @Transactional(rollbackFor = Exception.class, transactionManager = "transactionManager") 2. @Bean(name="transactionManager") public PlatformTransactionManager transactionManager((@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory){ JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(); jpaTransactionManager.setEntityManagerFactory(entityManagerFactory); return jpaTransactionManager; } 3. @Bean(name="entityManagerFactory") public EntityManagerFactory entityManager() { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setDatabase(Database.MYSQL); vendorAdapter.setGenerateDdl(false); vendorAdapter.setShowSql(true); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); factory.setDataSource(mysqlDataSource1()); factory.afterPropertiesSet(); return factory.getObject(); } 4. @Bean(name="mysqlDataSource1") @ConfigurationProperties(prefix="spring.datasource") public DataSource mysqlDataSource1() { return DataSourceBuilder.create().build(); } 5. 配置文件里设置:spring.datasource.hikari.transaction-isolation=READ_UNCOMMITTED 或者代码里自己设置 DataSourceProperty |
6
dust0522 OP @OldCarMan 你提醒了我,解决了哈哈,我用的 Druid 连接池 在配置文件中加入 spring.shardingsphere.datasource.defaultTransactionIsolation=4 (4 代表 REPEATABLE-READ), 这样连接池在创建连接的时候,会把这个值赋给 ShardingSphereConnection 。
|
7
dust0522 OP spring.shardingsphere.datasource 利用反射初始化的连接池的创建参数,我一直没想连接池的事
|