How to use @Transactional annotation in mybatis-spring?

后端 未结 1 1587
抹茶落季
抹茶落季 2021-02-15 23:15

I tried to use @Transactional annotation in spring and mybatis using mybatis-spring dependency. This is the service layer.

1条回答
  •  隐瞒了意图╮
    2021-02-16 00:14

    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.

    0 讨论(0)
提交回复
热议问题