Browse Source

去除SqlRunner持有的sqlSessionFactory变量.

nieqiurong 1 year ago
parent
commit
8ceed67f77

+ 0 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java

@@ -634,9 +634,4 @@ public class TableInfoHelper {
         return new SelectKeyGenerator(mappedStatement, true);
     }
 
-    public static void clearCache(){
-        TABLE_INFO_CACHE.clear();
-        TABLE_NAME_INFO_CACHE.clear();
-    }
-
 }

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

@@ -230,7 +230,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
      */
     @Deprecated
     protected boolean executeBatch(Consumer<SqlSession> consumer) {
-        return SqlHelper.executeBatch(this.sqlSessionFactory, this.entityClass, this.log, consumer);
+        return SqlHelper.executeBatch(this.sqlSessionFactory, this.log, consumer);
     }
 
     /**
@@ -244,7 +244,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
      * @since 3.3.1
      */
     protected <E> boolean executeBatch(Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
-        return SqlHelper.executeBatch(this.sqlSessionFactory, this.entityClass, this.log, list, batchSize, consumer);
+        return SqlHelper.executeBatch(this.sqlSessionFactory, this.log, list, batchSize, consumer);
     }
 
     /**

+ 0 - 6
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/spring/MybatisSqlSessionFactoryBean.java

@@ -439,15 +439,9 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
         notNull(dataSource, "Property 'dataSource' is required");
         state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
             "Property 'configuration' and 'configLocation' can not specified with together");
-        clearCache();
         this.sqlSessionFactory = buildSqlSessionFactory();
     }
 
-    protected void clearCache() throws IOException {
-        //TODO 清理掉资源  建议不要保留这个玩意了
-        SqlRunner.DEFAULT.close();
-        TableInfoHelper.clearCache();
-    }
 
     /**
      * Build a {@code SqlSessionFactory} instance.

+ 3 - 3
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/Db.java

@@ -95,7 +95,7 @@ public class Db {
         Class<T> entityClass = getEntityClass(entityList);
         Class<?> mapperClass = ClassUtils.toClassConfident(getTableInfo(entityClass).getCurrentNamespace());
         String sqlStatement = SqlHelper.getSqlStatement(mapperClass, SqlMethod.INSERT_ONE);
-        return SqlHelper.executeBatch(entityClass, LogFactory.getLog(Db.class), entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
+        return SqlHelper.executeBatch(entityClass, log, entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
     }
 
     /**
@@ -122,7 +122,7 @@ public class Db {
         Class<?> mapperClass = ClassUtils.toClassConfident(tableInfo.getCurrentNamespace());
         String keyProperty = tableInfo.getKeyProperty();
         Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for primary key from entity!");
-        return SqlHelper.saveOrUpdateBatch(entityClass, mapperClass, LogFactory.getLog(Db.class), entityList, batchSize, (sqlSession, entity) -> {
+        return SqlHelper.saveOrUpdateBatch(entityClass, mapperClass, log, entityList, batchSize, (sqlSession, entity) -> {
             Object idVal = tableInfo.getPropertyValue(entity, keyProperty);
             return StringUtils.checkValNull(idVal)
                 || CollectionUtils.isEmpty(sqlSession.selectList(SqlHelper.getSqlStatement(mapperClass, SqlMethod.SELECT_BY_ID), entity));
@@ -214,7 +214,7 @@ public class Db {
         Class<T> entityClass = getEntityClass(entityList);
         TableInfo tableInfo = getTableInfo(entityClass);
         String sqlStatement = SqlHelper.getSqlStatement(ClassUtils.toClassConfident(tableInfo.getCurrentNamespace()), SqlMethod.UPDATE_BY_ID);
-        return SqlHelper.executeBatch(entityClass, LogFactory.getLog(Db.class), entityList, batchSize, (sqlSession, entity) -> {
+        return SqlHelper.executeBatch(entityClass, log, entityList, batchSize, (sqlSession, entity) -> {
             MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
             param.put(Constants.ENTITY, entity);
             sqlSession.update(sqlStatement, param);

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

@@ -169,11 +169,11 @@ public final class SqlHelper {
      */
     @Deprecated
     public static boolean executeBatch(Class<?> entityClass, Log log, Consumer<SqlSession> consumer) {
-        return executeBatch(sqlSessionFactory(entityClass), entityClass, log, consumer);
+        return executeBatch(sqlSessionFactory(entityClass), log, consumer);
     }
 
     @SneakyThrows
-    public static boolean executeBatch(SqlSessionFactory sqlSessionFactory, Class<?> entityClass, Log log, Consumer<SqlSession> consumer) {
+    public static boolean executeBatch(SqlSessionFactory sqlSessionFactory, Log log, Consumer<SqlSession> consumer) {
         SqlSessionHolder sqlSessionHolder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sqlSessionFactory);
         boolean transaction = TransactionSynchronizationManager.isSynchronizationActive();
         if (sqlSessionHolder != null) {
@@ -219,16 +219,16 @@ public final class SqlHelper {
      * @param <E>         T
      * @return 操作结果
      * @since 3.4.0
-     * @deprecated {@link #executeBatch(SqlSessionFactory, Class, Log, Collection, int, BiConsumer)}
+     * @deprecated {@link #executeBatch(SqlSessionFactory, Log, Collection, int, BiConsumer)}
      */
     @Deprecated
     public static <E> boolean executeBatch(Class<?> entityClass, Log log, Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
-        return executeBatch(sqlSessionFactory(entityClass), entityClass, log, list, batchSize, consumer);
+        return executeBatch(sqlSessionFactory(entityClass), log, list, batchSize, consumer);
     }
 
-    public static <E> boolean executeBatch(SqlSessionFactory sqlSessionFactory, Class<?> entityClass, Log log, Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
+    public static <E> boolean executeBatch(SqlSessionFactory sqlSessionFactory, Log log, Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
         Assert.isFalse(batchSize < 1, "batchSize must not be less than one");
-        return !CollectionUtils.isEmpty(list) && executeBatch(sqlSessionFactory, entityClass, log, sqlSession -> {
+        return !CollectionUtils.isEmpty(list) && executeBatch(sqlSessionFactory, log, sqlSession -> {
             int size = list.size();
             int idxLimit = Math.min(batchSize, size);
             int i = 1;

+ 7 - 12
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlRunner.java

@@ -27,7 +27,6 @@ import org.apache.ibatis.session.SqlSessionFactory;
 import org.mybatis.spring.SqlSessionUtils;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.io.Closeable;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -38,18 +37,15 @@ import java.util.Optional;
  * @author Caratacus
  * @since 2016-12-11
  */
-public class SqlRunner implements ISqlRunner, Closeable {
+public class SqlRunner implements ISqlRunner {
 
     private final Log log = LogFactory.getLog(SqlRunner.class);
     // 单例Query
     public static final SqlRunner DEFAULT = new SqlRunner();
-    // 默认FACTORY
-    private SqlSessionFactory sqlSessionFactory;
 
     private Class<?> clazz;
 
     public SqlRunner() {
-        this.sqlSessionFactory = SqlHelper.FACTORY;
     }
 
     public SqlRunner(Class<?> clazz) {
@@ -62,10 +58,6 @@ public class SqlRunner implements ISqlRunner, Closeable {
      * @return ignore
      */
     public static SqlRunner db() {
-        // 初始化的静态变量 还是有前后加载的问题 该判断只会执行一次
-        if (DEFAULT.sqlSessionFactory == null) {
-            DEFAULT.sqlSessionFactory = SqlHelper.FACTORY;
-        }
         return DEFAULT;
     }
 
@@ -238,12 +230,15 @@ public class SqlRunner implements ISqlRunner, Closeable {
      * 获取SqlSessionFactory
      */
     private SqlSessionFactory getSqlSessionFactory() {
-        return Optional.ofNullable(clazz).map(GlobalConfigUtils::currentSessionFactory).orElse(sqlSessionFactory);
+        return Optional.ofNullable(clazz).map(GlobalConfigUtils::currentSessionFactory).orElse(SqlHelper.FACTORY);
     }
 
-    @Override
+    /**
+     * @deprecated 3.5.3
+     */
+    @Deprecated
     public void close() {
-        DEFAULT.sqlSessionFactory = null;
+
     }
 
 }

+ 0 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/BaseDbTest.java

@@ -42,7 +42,6 @@ public abstract class BaseDbTest<T> extends TypeReference<T> {
 
     @SuppressWarnings("unchecked")
     public BaseDbTest() {
-        SqlRunner.DEFAULT.close();
         DataSource ds = dataSource();
         List<String> tableSql = tableSql();
         String tableDataSql = tableDataSql();

+ 0 - 12
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/BaseTest.java

@@ -15,18 +15,13 @@
  */
 package com.baomidou.mybatisplus.test.h2;
 
-import com.baomidou.mybatisplus.core.metadata.TableInfo;
-import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
 import com.baomidou.mybatisplus.test.h2.entity.H2User;
 import com.baomidou.mybatisplus.test.h2.enums.AgeEnum;
-import org.junit.jupiter.api.AfterAll;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.test.annotation.DirtiesContext;
 
 import javax.annotation.Resource;
-import java.lang.reflect.Field;
 import java.util.List;
-import java.util.Map;
 
 @DirtiesContext
 public class BaseTest {
@@ -55,11 +50,4 @@ public class BaseTest {
         });
     }
 
-    @AfterAll
-    public static void afterAll() throws NoSuchFieldException, IllegalAccessException {
-        Field tableInfoCache = TableInfoHelper.class.getDeclaredField("TABLE_INFO_CACHE");
-        tableInfoCache.setAccessible(true);
-        Map<Class<?>, TableInfo> tableInfoMap = (Map<Class<?>, TableInfo>) tableInfoCache.get(TableInfoHelper.class);
-        tableInfoMap.clear();
-    }
 }