--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.core.persistence;
+
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * @since 2.13
+ */
+public final class DatabaseUtils {
+ private DatabaseUtils() {
+ }
+
+ public static void closeQuietly(@Nullable Connection connection) {
+ if (connection != null) {
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ LoggerFactory.getLogger(DatabaseUtils.class).warn("Fail to close connection", e);
+ // ignore
+ }
+ }
+ }
+
+ public static void closeQuietly(@Nullable Statement stmt) {
+ if (stmt != null) {
+ try {
+ stmt.close();
+ } catch (SQLException e) {
+ LoggerFactory.getLogger(DatabaseUtils.class).warn("Fail to close statement", e);
+ // ignore
+ }
+ }
+ }
+
+ public static void closeQuietly(@Nullable ResultSet rs) {
+ if (rs != null) {
+ try {
+ rs.close();
+ } catch (SQLException e) {
+ LoggerFactory.getLogger(DatabaseUtils.class).warn("Fail to close result set", e);
+ // ignore
+ }
+ }
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.core.persistence;
+
+import org.junit.Test;
+import org.sonar.core.persistence.dialect.Oracle;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class DatabaseUtilsTest extends AbstractDaoTestCase {
+
+ @Test
+ public void should_close_connection() throws SQLException {
+ Connection connection = getConnection();
+ assertThat(isClosed(connection)).isFalse();
+
+ DatabaseUtils.closeQuietly(connection);
+ assertThat(isClosed(connection)).isTrue();
+ }
+
+ @Test
+ public void should_support_null_connection() {
+ DatabaseUtils.closeQuietly((Connection) null);
+ // no failure
+ }
+
+ @Test
+ public void should_close_statement_and_resultset() throws SQLException {
+ Connection connection = getConnection();
+ try {
+ PreparedStatement statement = connection.prepareStatement(selectDual());
+ ResultSet rs = statement.executeQuery();
+
+ DatabaseUtils.closeQuietly(rs);
+ DatabaseUtils.closeQuietly(statement);
+
+ assertThat(isClosed(statement)).isTrue();
+ assertThat(isClosed(rs)).isTrue();
+ } finally {
+ DatabaseUtils.closeQuietly(connection);
+ }
+ }
+
+ @Test
+ public void should_not_fail_on_connection_errors() throws SQLException {
+ Connection connection = mock(Connection.class);
+ doThrow(new SQLException()).when(connection).close();
+
+ DatabaseUtils.closeQuietly(connection);
+
+ // no failure
+ verify(connection).close(); // just to be sure
+ }
+
+ @Test
+ public void should_not_fail_on_statement_errors() throws SQLException {
+ Statement statement = mock(Statement.class);
+ doThrow(new SQLException()).when(statement).close();
+
+ DatabaseUtils.closeQuietly(statement);
+
+ // no failure
+ verify(statement).close(); // just to be sure
+ }
+
+ @Test
+ public void should_not_fail_on_resulset_errors() throws SQLException {
+ ResultSet rs = mock(ResultSet.class);
+ doThrow(new SQLException()).when(rs).close();
+
+ DatabaseUtils.closeQuietly(rs);
+
+ // no failure
+ verify(rs).close(); // just to be sure
+ }
+
+ /**
+ * Connection.isClosed() has been introduced in java 1.6
+ */
+ private boolean isClosed(Connection c) {
+ try {
+ c.createStatement().execute(selectDual());
+ return false;
+ } catch (Exception e) {
+ return true;
+ }
+ }
+
+ /**
+ * Statement.isClosed() has been introduced in java 1.6
+ */
+ private boolean isClosed(Statement s) {
+ try {
+ s.execute("SELECT 1");
+ return false;
+ } catch (Exception e) {
+ return true;
+ }
+ }
+
+ /**
+ * ResultSet.isClosed() has been introduced in java 1.6
+ */
+ private boolean isClosed(ResultSet rs) {
+ try {
+ rs.next();
+ return false;
+ } catch (Exception e) {
+ return true;
+ }
+ }
+
+ private String selectDual() {
+ String sql = "SELECT 1";
+ if (Oracle.ID.equals(getDatabase().getDialect().getId())) {
+ sql = "SELECT 1 FROM DUAL";
+ }
+ return sql;
+ }
+}