Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using mybatis api participate in spring transaction #531

Open
linweichao opened this issue Oct 17, 2020 · 3 comments
Open

using mybatis api participate in spring transaction #531

linweichao opened this issue Oct 17, 2020 · 3 comments
Assignees

Comments

@linweichao
Copy link

the Using the MyBatis API doc says:

  • It will not participate in any Spring transactions.
  • If the SqlSession is using a DataSource that is also being used by a Spring transaction manager and there is currently a transaction in progress, this code will throw an exception.

but my test produces different result:

  • using api does participate in Spring transactions
  • not throw exception

below is the test code, use mybatis-spring-boot-starter:2.1.3, keep the default auto configuration, the table is simple
when the exception throws, all inserts roll back
does the doc wrong? or I misunderstand something?

package com.example.demo;

import java.util.Arrays;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class TransactionTest implements ApplicationRunner {

    /**
     * auto configure one
     */
    @Autowired
    private SqlSessionFactory SqlSessionFactory;

    /**
     * auto configure one
     */
    @Autowired
    private PersonMapper personMapper;

    @Override
    @Transactional
    public void run(ApplicationArguments args) throws Exception {
        personMapper.insertPerson("mike", 18);

        try(SqlSession sqlSession = SqlSessionFactory.openSession(ExecutorType.BATCH)) {
            List<String> dogNames = Arrays.asList("aaa", "bbb");
            DogMapper dogMapper = sqlSession.getMapper(DogMapper.class);
            for (String name: dogNames) {
                dogMapper.insertDog(name);
            }
            sqlSession.commit();
        }
        if (true) {
            throw new RuntimeException();
        }
    }

    @Mapper
    public interface PersonMapper {
        @Insert("insert into person(name, age) values(#{name}, #{age})")
        void insertPerson(@Param("name") String name, @Param("age") int age);
    }

    @Mapper
    public interface DogMapper {
        @Insert("insert into dog(name) values(#{name})")
        void insertDog(String name);
    }
    
}
@kazuki43zoo
Copy link
Member

@linweichao

Thanks you for your reporting!
I'll investigate this. I have one question. Does the current behavior cause some problems with your application ?

@kazuki43zoo kazuki43zoo self-assigned this Nov 10, 2020
@linweichao
Copy link
Author

It does not cause problem. This behavior is what I need.

@kazuki43zoo
Copy link
Member

Should fix documentation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants