Browse Source

关闭流对象

Caratacus 8 years ago
parent
commit
cdb6f774d0

+ 3 - 11
mybatis-plus/src/main/java/com/baomidou/mybatisplus/MybatisSessionFactoryBuilder.java

@@ -19,12 +19,12 @@ import com.baomidou.mybatisplus.annotations.FieldStrategy;
 import com.baomidou.mybatisplus.mapper.DBType;
 import com.baomidou.mybatisplus.mapper.IMetaObjectHandler;
 import com.baomidou.mybatisplus.mapper.ISqlInjector;
+import com.baomidou.mybatisplus.toolkit.IOUtils;
 import org.apache.ibatis.exceptions.ExceptionFactory;
 import org.apache.ibatis.executor.ErrorContext;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
 import java.util.Properties;
@@ -48,11 +48,7 @@ public class MybatisSessionFactoryBuilder extends SqlSessionFactoryBuilder {
 			throw ExceptionFactory.wrapException("Error building SqlSession.", e);
 		} finally {
 			ErrorContext.instance().reset();
-			try {
-				reader.close();
-			} catch (IOException e) {
-				// Intentionally ignore. Prefer previous error.
-			}
+			IOUtils.closeQuietly(reader);
 		}
 	}
 
@@ -65,11 +61,7 @@ public class MybatisSessionFactoryBuilder extends SqlSessionFactoryBuilder {
 			throw ExceptionFactory.wrapException("Error building SqlSession.", e);
 		} finally {
 			ErrorContext.instance().reset();
-			try {
-				inputStream.close();
-			} catch (IOException e) {
-				// Intentionally ignore. Prefer previous error.
-			}
+			IOUtils.closeQuietly(inputStream);
 		}
 	}
 

+ 0 - 1
mybatis-plus/src/main/java/com/baomidou/mybatisplus/plugins/PaginationInterceptor.java

@@ -42,7 +42,6 @@ import org.apache.ibatis.session.RowBounds;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
-import java.sql.SQLException;
 import java.util.Properties;
 
 /**

+ 21 - 25
mybatis-plus/src/main/java/com/baomidou/mybatisplus/plugins/SqlExplainInterceptor.java

@@ -15,12 +15,8 @@
  */
 package com.baomidou.mybatisplus.plugins;
 
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.util.Properties;
-import java.util.logging.Logger;
-
+import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
+import com.baomidou.mybatisplus.toolkit.IOUtils;
 import org.apache.ibatis.builder.StaticSqlSource;
 import org.apache.ibatis.executor.Executor;
 import org.apache.ibatis.mapping.BoundSql;
@@ -34,7 +30,11 @@ import org.apache.ibatis.plugin.Signature;
 import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
 import org.apache.ibatis.session.Configuration;
 
-import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.Properties;
+import java.util.logging.Logger;
 
 /**
  * <p>
@@ -44,15 +44,15 @@ import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
  * @author hubin
  * @Date 2016-08-16
  */
-@Intercepts({@Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
+@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
 public class SqlExplainInterceptor implements Interceptor {
 	protected final Logger logger = Logger.getLogger("SqlExplainInterceptor");
-	
+
 	/**
 	 * 发现执行全表 delete update 语句是否停止执行
 	 */
 	private boolean stopProceed = false;
-	
+
 	public Object intercept(Invocation invocation) throws Throwable {
 		/**
 		 * 处理 DELETE UPDATE 语句
@@ -77,6 +77,7 @@ public class SqlExplainInterceptor implements Interceptor {
 	 * <p>
 	 * 判断是否执行 SQL
 	 * </p>
+	 * 
 	 * @param configuration
 	 * @param mappedStatement
 	 * @param boundSql
@@ -85,8 +86,8 @@ public class SqlExplainInterceptor implements Interceptor {
 	 * @return
 	 * @throws Exception
 	 */
-	protected void sqlExplain( Configuration configuration, MappedStatement mappedStatement, BoundSql boundSql,
-			Connection connection, Object parameter ) throws Exception {
+	protected void sqlExplain(Configuration configuration, MappedStatement mappedStatement, BoundSql boundSql,
+			Connection connection, Object parameter) {
 		PreparedStatement stmt = null;
 		ResultSet rs = null;
 		try {
@@ -94,14 +95,16 @@ public class SqlExplainInterceptor implements Interceptor {
 			explain.append(boundSql.getSql());
 			String sqlExplain = explain.toString();
 			StaticSqlSource sqlsource = new StaticSqlSource(configuration, sqlExplain, boundSql.getParameterMappings());
-			MappedStatement.Builder builder = new MappedStatement.Builder(configuration, "explain_sql", sqlsource, SqlCommandType.SELECT);
-			builder.resultMaps(mappedStatement.getResultMaps()).resultSetType(mappedStatement.getResultSetType()).statementType(mappedStatement.getStatementType());
+			MappedStatement.Builder builder = new MappedStatement.Builder(configuration, "explain_sql", sqlsource,
+					SqlCommandType.SELECT);
+			builder.resultMaps(mappedStatement.getResultMaps()).resultSetType(mappedStatement.getResultSetType())
+					.statementType(mappedStatement.getStatementType());
 			MappedStatement query_statement = builder.build();
 			DefaultParameterHandler handler = new DefaultParameterHandler(query_statement, parameter, boundSql);
 			stmt = connection.prepareStatement(sqlExplain);
 			handler.setParameters(stmt);
 			rs = stmt.executeQuery();
-			while ( rs.next() ) {
+			while (rs.next()) {
 				if (!"Using where".equals(rs.getString("Extra"))) {
 					String tip = " Full table operation is prohibited. SQL: " + boundSql.getSql();
 					if (this.isStopProceed()) {
@@ -112,17 +115,10 @@ public class SqlExplainInterceptor implements Interceptor {
 				}
 			}
 
-		} catch ( Exception e ) {
-			throw new MybatisPlusException(e); 
+		} catch (Exception e) {
+			throw new MybatisPlusException(e);
 		} finally {
-			if ( rs != null ) {
-				rs.close();
-				rs = null;
-			}
-			if ( stmt != null ) {
-				stmt.close();
-				stmt = null;
-			}
+			IOUtils.closeQuietly(rs, stmt);
 		}
 	}