package org.sonar.core.measure;
import org.apache.ibatis.session.SqlSession;
+import org.slf4j.LoggerFactory;
import org.sonar.api.ServerComponent;
import org.sonar.core.persistence.Database;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.resource.ResourceDao;
+import javax.annotation.Nullable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
public List<MeasureFilterRow> execute(MeasureFilter filter, MeasureFilterContext context) throws SQLException {
List<MeasureFilterRow> rows;
SqlSession session = null;
+ Connection connection = null;
try {
session = mybatis.openSession();
prepareContext(context, filter, session);
if (isValid(filter, context)) {
MeasureFilterSql sql = new MeasureFilterSql(database, filter, context);
context.setSql(sql.sql());
- Connection connection = session.getConnection();
+ connection = session.getConnection();
rows = sql.execute(connection);
} else {
rows = Collections.emptyList();
}
} finally {
MyBatis.closeQuietly(session);
+ // connection is supposed to be closed by the session
+ closeQuietly(connection);
}
return rows;
}
+ private void closeQuietly(@Nullable Connection connection) {
+ if (connection != null) {
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ LoggerFactory.getLogger(MeasureFilterExecutor.class).warn("Fail to close connection", e);
+ // ignore
+ }
+ }
+ }
+
private void prepareContext(MeasureFilterContext context, MeasureFilter filter, SqlSession session) {
if (filter.getBaseResourceKey() != null) {