Skip to content

REQUIRED中嵌套REQUIRES_NEW时,满足一定条件必定报错"connectionProxyMap" is null #763

@JustryDeng

Description

@JustryDeng

Describe the bug
REQUIRED中嵌套REQUIRES_NEW时,若REQUIRED中存在jdbc连接,但REQUIRES_NEW中不存在jdbc连接,必定触发报错"connectionProxyMap" is null
注:不论是通过注解DSTransactional还是TransactionalTemplate,都会有这个问题

To Reproduce
本人使用的版本:4.3.1

      <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
        <version>4.3.1</version>
      </dependency>

复现代码:

@SpringBootTest
class NonTransactionTests {
    
    @Autowired
    NonDatabaseConnectionService nonDatabaseConnectionService;
    
    /**
     * 复现问题
     */
    @Test
    void test1() {
        nonDatabaseConnectionService.reProduce();
    }
    
}
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.dynamic.datasource.tx.DsPropagation;
import com.ideaaedi.zoo.example.springboot.dynamic.datasource.mapper.MysqlStudentMapper;
import jakarta.annotation.Resource;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadLocalRandom;

@Component
public class NonDatabaseConnectionService {
    
    @Resource
    private MysqlStudentMapper mysqlStudentMapper;
    
    /**
     * 问题复现
     */
    @DSTransactional(propagation = DsPropagation.REQUIRED)
    public void reProduce() {
        triggerJdbcConnection();
        // 获取aop对象,确保bizLogic的aop生效
        ((NonDatabaseConnectionService) AopContext.currentProxy()).bizLogic();
    }
    
    /**
     * 触发jdbc连接
     */
    public void triggerJdbcConnection() {
        /*
         * 调试时发现,只有当触发了jdbc connection时,dynamic-datasource才会往ConnectionFactory.CONNECTION_HOLDER中放置事务连接信息
         */
        mysqlStudentMapper.list();
    }
    
    /**
     * 业务逻辑(模拟:满足某些业务条件时,可能不会有任何与数据库有关的操作)
     */
    @DSTransactional(propagation = DsPropagation.REQUIRES_NEW)
    public void bizLogic() {
        if (ThreadLocalRandom.current().nextBoolean()) {
            /*
             * bug反馈:如果代码走到这个if中,那么就会报错:
             *
             * java.lang.NullPointerException: Cannot invoke "java.util.Map.values()" because "connectionProxyMap" is null
             *
             * 	at com.baomidou.dynamic.datasource.tx.ConnectionFactory.notify(ConnectionFactory.java:150)
             * 	at com.baomidou.dynamic.datasource.tx.LocalTxUtil.commit(LocalTxUtil.java:91)
             * 	at com.baomidou.dynamic.datasource.tx.TransactionalTemplate.doExecute(TransactionalTemplate.java:126)
             * 	at com.baomidou.dynamic.datasource.tx.TransactionalTemplate.execute(TransactionalTemplate.java:94)
             * 	at com.baomidou.dynamic.datasource.aop.DynamicLocalTransactionInterceptor.invoke(DynamicLocalTransactionInterceptor.java:59)
             * 	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
             */
            System.err.println("bizLogic");
        } else {
            triggerJdbcConnection();
        }
    }
}

Expected behavior
期望如果方法不涉及jdbc操作的话,一切正常,不报错NPE

Screenshots

Desktop (please complete the following information):

  • OS: WINDOWS10

Smartphone (please complete the following information):

Additional context

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions