{"action":"created","issue":{"url":"https://api.github.com/repos/baomidou/mybatis-plus/issues/732","repository_url":"https://api.github.com/repos/baomidou/mybatis-plus","labels_url":"https://api.github.com/repos/baomidou/mybatis-plus/issues/732/labels{/name}","comments_url":"https://api.github.com/repos/baomidou/mybatis-plus/issues/732/comments","events_url":"https://api.github.com/repos/baomidou/mybatis-plus/issues/732/events","html_url":"https://github.com/baomidou/mybatis-plus/issues/732","id":393944579,"node_id":"MDU6SXNzdWUzOTM5NDQ1Nzk=","number":732,"title":"执行单元测试事务不回滚","user":{"login":"zhenyuT","id":9477681,"node_id":"MDQ6VXNlcjk0Nzc2ODE=","avatar_url":"https://avatars3.githubusercontent.com/u/9477681?v=4","gravatar_id":"","url":"https://api.github.com/users/zhenyuT","html_url":"https://github.com/zhenyuT","followers_url":"https://api.github.com/users/zhenyuT/followers","following_url":"https://api.github.com/users/zhenyuT/following{/other_user}","gists_url":"https://api.github.com/users/zhenyuT/gists{/gist_id}","starred_url":"https://api.github.com/users/zhenyuT/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zhenyuT/subscriptions","organizations_url":"https://api.github.com/users/zhenyuT/orgs","repos_url":"https://api.github.com/users/zhenyuT/repos","events_url":"https://api.github.com/users/zhenyuT/events{/privacy}","received_events_url":"https://api.github.com/users/zhenyuT/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-12-25T01:34:25Z","updated_at":"2018-12-27T01:31:47Z","closed_at":null,"author_association":"NONE","body":"### 当前使用版本(必须填写清楚，否则不予处理)\r\n版本:3.0.6\r\n\r\n\r\n### 该问题是怎么引起的？**([最新版](https://search.maven.org/search?q=mybatis-plus)上已修复的会直接**close**掉)**\r\n\r\n继承IService接口，调用saveBatch方法。\r\n\r\n单元测试，加了@transactional @Rollback注解，直接在当前xxxTest类中执行，事务可以回滚；通过mvn test命令执行(一共90个单元测试)，某些测试方法的事务不回滚，数据直接插入到数据库了。\r\n\r\n将com.baomidou.mybatisplus.extension.service.impl.ServiceImpl类的sqlSessionBatch方法 :\r\n```\r\n    /**\r\n     * <p>\r\n     * 批量操作 SqlSession\r\n     * </p>\r\n     */\r\n    protected SqlSession sqlSessionBatch() {\r\n        return SqlHelper.sqlSessionBatch(currentModelClass());\r\n    }\r\n```\r\n修改为：\r\n\r\n```\r\n    @Autowired\r\n    @Qualifier(\"customSqlSessionFactory\")\r\n    SqlSessionFactory sqlSessionFactory;\r\n\r\n    /**\r\n     * <p>\r\n     * 批量操作 SqlSession\r\n     * </p>\r\n     */\r\n    protected SqlSession sqlSessionBatch() {\r\n        return sqlSessionFactory.openSession(ExecutorType.BATCH);\r\n    }\r\n```\r\n\r\n直接通过注解注入SqlSessionFactory后，这个问题得到解决。\r\n\r\n但是我们的项目中是用到了多数据源的，这种方式虽然能解决问题，但是个人并不是很喜欢。\r\n还有通过SqlHelper.sqlSessionBatch(currentModelClass())的方式获取SqlSession的做法，是什么原因导致出的问题呢？？？\r\n\r\n### 重现步骤\r\n\r\n继承IService接口调用saveBatch方法，通过mvn test命令执行单元测试。\r\n\r\n### 报错信息\r\n\r\n事务不回滚\r\n"},"comment":{"url":"https://api.github.com/repos/baomidou/mybatis-plus/issues/comments/450051713","html_url":"https://github.com/baomidou/mybatis-plus/issues/732#issuecomment-450051713","issue_url":"https://api.github.com/repos/baomidou/mybatis-plus/issues/732","id":450051713,"node_id":"MDEyOklzc3VlQ29tbWVudDQ1MDA1MTcxMw==","user":{"login":"zhenyuT","id":9477681,"node_id":"MDQ6VXNlcjk0Nzc2ODE=","avatar_url":"https://avatars3.githubusercontent.com/u/9477681?v=4","gravatar_id":"","url":"https://api.github.com/users/zhenyuT","html_url":"https://github.com/zhenyuT","followers_url":"https://api.github.com/users/zhenyuT/followers","following_url":"https://api.github.com/users/zhenyuT/following{/other_user}","gists_url":"https://api.github.com/users/zhenyuT/gists{/gist_id}","starred_url":"https://api.github.com/users/zhenyuT/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zhenyuT/subscriptions","organizations_url":"https://api.github.com/users/zhenyuT/orgs","repos_url":"https://api.github.com/users/zhenyuT/repos","events_url":"https://api.github.com/users/zhenyuT/events{/privacy}","received_events_url":"https://api.github.com/users/zhenyuT/received_events","type":"User","site_admin":false},"created_at":"2018-12-27T01:31:47Z","updated_at":"2018-12-27T01:31:47Z","author_association":"NONE","body":"> 没什么风险,本身就不推荐以及不支持创建多个sqlsessionfactory公用相同的mapper.\r\n\r\n我这边没有SqlSessionFactory公用相同的mapper的情况哦，虽然有多个SqlSessionFactory，但是每个mapper对应的SqlSessionFactory都是固定的。\r\n\r\n之所以出现事务不回滚的原因，是因为SqlSessionFactory对象被重新创建了(原因未知)，而model缓存的SqlSessionFactory还是旧的。\r\n\r\n对于Spring+Junit+Mockito的实现原理不是很清楚，但是看日志发现Spring boot和mybatis-plus的启动logo有被重复打印的，这是否说明整个Spring容器都被重新创建了？"}}
{"actor":{"display_login":"zhenyuT"}}