I tried to use @Transactional
annotation in spring
and mybatis
using mybatis-spring
dependency. This is the service layer.
As @M.Deinum metioned, I have to make the method where @Transactional
applied public, in other words, I have to change
@Transactional(propagation = Propagation.REQUIRED)
void insert1() {
TestModel testModel = new TestModel(1, "title1", "content1");
mapper.insert(testModel);
throw new RuntimeException();
}
to
@Transactional(propagation = Propagation.REQUIRED)
public void insert1() {
TestModel testModel = new TestModel(1, "title1", "content1");
mapper.insert(testModel);
throw new RuntimeException();
}
The reason is written in the spring documentation.
Method visibility and @Transactional
When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ (see below) if you need to annotate non-public methods.