spring-boot-starter: About the problems encountered in using declarative transactions in spring-mybatis
Hello, dear partner: I have a problem that has bothered me recently. I want to consult here. We all know that spring’s transaction management is due to the cglib proxy problem. When methods in the same class call each other or the called method is private, the transaction will be invalid. This is a bug that is easy to appear on other orm frameworks. Such as hibernate, jpa, etc. But the following sample code can be rolled back normally in springboot-mybatis.
this is mybatis mapper code:
public interface TestMapper() {
@Update("update table set field = '123' where id = 123)");
int testUpdate();
}
this is the code of a test class, which contains a private method b, and method a is controlled by the spring transaction manager:
@Service
public class testA() {
@Autowried
TestMapper testMapper;
@Transactional(rollBackFor = Exception.class)
public void a() {
b();
}
private void b() {
testMapper.testUpdate();
int a = 1/0;
}
}
When the a method of the testA class is called, the b method will be called and an exception that the divisor cannot be 0 is thrown, the strange thing was actually rolled back. When method b belongs to a method in another class and does not use spring’s declarative transaction management, the same result will be obtained. When I debug the source code, I found that the SqlSessionUtils class handles the relevant logic. I would like to ask whether the transaction is related to the current thread in the spring-mybatis project, that is to say, all the method chains called by the same thread share a transaction? Looking forward to the answer, thanks :)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (7 by maintainers)
I understand. thank u