Bläddra i källkod

手动进行开启与释放,运行日志中就不会看到 Closing non transactional SqlSession 与 Creating a new SqlSession

聂秋秋 6 år sedan
förälder
incheckning
f330203e62

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

@@ -133,8 +133,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         if (CollectionUtils.isEmpty(entityList)) {
             throw new IllegalArgumentException("Error: entityList must not be empty");
         }
-        SqlSession batchSqlSession = sqlSessionBatch();
-        try {
+        try(SqlSession batchSqlSession = sqlSessionBatch()) {
             int size = entityList.size();
             String sqlStatement = sqlStatement(SqlMethod.INSERT_ONE);
             for (int i = 0; i < size; i++) {
@@ -144,8 +143,6 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
                 }
             }
             batchSqlSession.flushStatements();
-        } finally {
-            closeSqlSession(batchSqlSession);
         }
         return true;
     }
@@ -240,8 +237,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         if (CollectionUtils.isEmpty(entityList)) {
             throw new IllegalArgumentException("Error: entityList must not be empty");
         }
-        SqlSession batchSqlSession = sqlSessionBatch();
-        try {
+        try(SqlSession batchSqlSession = sqlSessionBatch()) {
             int size = entityList.size();
             Class<?> cls = null;
             TableInfo tableInfo = null;
@@ -270,8 +266,6 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
                 }
             }
             batchSqlSession.flushStatements();
-        } finally {
-            closeSqlSession(batchSqlSession);
         }
         return true;
     }
@@ -363,8 +357,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
         if (CollectionUtils.isEmpty(entityList)) {
             throw new IllegalArgumentException("Error: entityList must not be empty");
         }
-        SqlSession batchSqlSession = sqlSessionBatch();
-        try {
+        try(SqlSession batchSqlSession = sqlSessionBatch()) {
             int size = entityList.size();
             SqlMethod sqlMethod = selective ? SqlMethod.UPDATE_BY_ID : SqlMethod.UPDATE_ALL_COLUMN_BY_ID;
             String sqlStatement = sqlStatement(sqlMethod);
@@ -377,8 +370,6 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
                 }
             }
             batchSqlSession.flushStatements();
-        } finally {
-            closeSqlSession(batchSqlSession);
         }
         return true;
     }