فهرست منبع

事物嵌套批量插入清理sqlSession缓存.

https://gitee.com/baomidou/mybatis-plus/issues/IZKBN
nieqiuqiu 5 سال پیش
والد
کامیت
03ecab16b2

+ 3 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/impl/ServiceImpl.java

@@ -114,6 +114,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     @Transactional(rollbackFor = Exception.class)
     @Override
     public boolean saveBatch(Collection<T> entityList, int batchSize) {
+        SqlHelper.clearCache(currentModelClass());
         String sqlStatement = sqlStatement(SqlMethod.INSERT_ONE);
         try (SqlSession batchSqlSession = sqlSessionBatch()) {
             int i = 0;
@@ -155,6 +156,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
         Assert.notEmpty(entityList, "error: entityList must not be empty");
         Class<?> cls = currentModelClass();
+        SqlHelper.clearCache(cls);
         TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
         Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
         String keyProperty = tableInfo.getKeyProperty();
@@ -217,6 +219,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
     public boolean updateBatchById(Collection<T> entityList, int batchSize) {
         Assert.notEmpty(entityList, "error: entityList must not be empty");
         String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID);
+        SqlHelper.clearCache(currentModelClass());
         try (SqlSession batchSqlSession = sqlSessionBatch()) {
             int i = 0;
             for (T anEntityList : entityList) {

+ 17 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java

@@ -25,7 +25,9 @@ import org.apache.ibatis.logging.LogFactory;
 import org.apache.ibatis.session.ExecutorType;
 import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
+import org.mybatis.spring.SqlSessionHolder;
 import org.mybatis.spring.SqlSessionUtils;
+import org.springframework.transaction.support.TransactionSynchronizationManager;
 
 import java.util.List;
 
@@ -149,4 +151,19 @@ public final class SqlHelper {
         }
         return null;
     }
+
+    /**
+     * 清理缓存.
+     * 批量插入因为无法重用sqlSession,只能新开启一个sqlSession
+     *
+     * @param clazz 实体类
+     */
+    public static void clearCache(Class<?> clazz){
+        SqlSessionFactory sqlSessionFactory = GlobalConfigUtils.currentSessionFactory(clazz);
+        SqlSessionHolder sqlSessionHolder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sqlSessionFactory);
+        if (sqlSessionHolder != null) {
+            SqlSession sqlSession = sqlSessionHolder.getSqlSession();
+            sqlSession.clearCache();
+        }
+    }
 }

+ 5 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2KeyGeneratorTest.java

@@ -35,26 +35,30 @@ class H2KeyGeneratorTest {
     private ExtendKeyGeneratorMapper extendKeyGeneratorMapper;
 
     @Test
-    void test(){
+    void test() {
         KeyGeneratorModel keyGeneratorModel = new KeyGeneratorModel();
         keyGeneratorModel.setName("我举起了咩咩");
         keyGeneratorMapper.insert(keyGeneratorModel);
         Assertions.assertNotNull(keyGeneratorModel.getUid());
+        Assertions.assertEquals(keyGeneratorModel.getUid(), 1L);
 
         LongKeyGeneratorModel longKeyGeneratorModel = new LongKeyGeneratorModel();
         longKeyGeneratorModel.setName("我举起了个栗子");
         longKeyGeneratorMapper.insert(longKeyGeneratorModel);
         Assertions.assertNotNull(longKeyGeneratorModel.getId());
+        Assertions.assertEquals(longKeyGeneratorModel.getId(), 2L);
 
         StringKeyGeneratorModel stringKeyGeneratorModel = new StringKeyGeneratorModel();
         stringKeyGeneratorModel.setName("我举起了个锤子");
         stringKeyGeneratorMapper.insert(stringKeyGeneratorModel);
         Assertions.assertNotNull(stringKeyGeneratorModel.getId());
+        Assertions.assertEquals(stringKeyGeneratorModel.getId(), "3");
 
         ExtendKeyGeneratorModel extendKeyGeneratorModel = new ExtendKeyGeneratorModel();
         extendKeyGeneratorModel.setName("我举起了句号");
         extendKeyGeneratorMapper.insert(extendKeyGeneratorModel);
         Assertions.assertNotNull(extendKeyGeneratorModel.getUid());
+        Assertions.assertEquals(extendKeyGeneratorModel.getUid(), 4L);
     }
 
 }

+ 14 - 4
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2UserTest.java

@@ -30,13 +30,11 @@ import org.junit.jupiter.api.extension.ExtendWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
 import java.math.RoundingMode;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * Mybatis Plus H2 Junit Test
@@ -356,6 +354,18 @@ class H2UserTest extends BaseTest {
         }
     }
 
+    @Test
+    @Order(29)
+    @Transactional
+    void testClearSqlSessionCache() {
+        H2User h2User;
+        h2User = userService.getById(996102919L);
+        assert h2User == null;
+        userService.saveBatch(Collections.singletonList(new H2User(996102919L, "靓仔")));
+        h2User = userService.getById(996102919L);
+        Assertions.assertNotNull(h2User);
+    }
+
     @Test
     public void myQueryWithGroupByOrderBy(){
         userService.mySelectMaps().forEach(System.out::println);

+ 1 - 1
mybatis-plus/src/test/resources/keygenerator/init.ddl.sql

@@ -4,4 +4,4 @@ CREATE TABLE IF NOT EXISTS  key_generator_model (
 	PRIMARY KEY (id)
 );
 
-CREATE SEQUENCE key_generator_model_seq START WITH 1 INCREMENT BY 10;
+CREATE SEQUENCE key_generator_model_seq START WITH 1 INCREMENT BY 1;