From e42d047aa8dced87793884ec2bf426b379a7173c Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 18 Oct 2012 11:06:44 +0200 Subject: [PATCH] Check db connection before Rails initialization in order to improve logs --- .../core/measure/MeasureFilterExecutor.java | 15 +------ .../sonar/core/measure/MeasureFilterSql.java | 32 ++++---------- .../sonar/core/persistence/DatabaseUtils.java | 42 +++++++++++++++++++ .../core/persistence/DefaultDatabase.java | 17 +++++++- 4 files changed, 69 insertions(+), 37 deletions(-) diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java index ebf5cec2809..6aac5b504a8 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java @@ -23,6 +23,7 @@ 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.DatabaseUtils; import org.sonar.core.persistence.MyBatis; import org.sonar.core.resource.ResourceDao; @@ -63,24 +64,12 @@ public class MeasureFilterExecutor implements ServerComponent { } finally { MyBatis.closeQuietly(session); // connection is supposed to be closed by the session - closeQuietly(connection); + DatabaseUtils.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) { context.setBaseSnapshot(resourceDao.getLastSnapshot(filter.getBaseResourceKey(), session)); diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java index 68015b1e07e..176d50f8e41 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java @@ -24,14 +24,19 @@ import com.google.common.collect.Lists; import com.google.common.collect.Ordering; import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; -import org.slf4j.LoggerFactory; import org.sonar.api.measures.Metric; import org.sonar.core.persistence.Database; +import org.sonar.core.persistence.DatabaseUtils; import org.sonar.core.persistence.dialect.PostgreSql; import org.sonar.core.resource.SnapshotDto; import javax.annotation.Nullable; -import java.sql.*; + +import java.sql.Connection; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.Collection; import java.util.List; @@ -61,7 +66,8 @@ class MeasureFilterSql { return process(rs); } finally { - closeQuietly(statement, rs); + DatabaseUtils.closeQuietly(rs); + DatabaseUtils.closeQuietly(statement); } } @@ -258,24 +264,4 @@ class MeasureFilterSql { to.append(StringUtils.join(values, "','")); to.append("') "); } - - private static void closeQuietly(@Nullable Statement stmt, @Nullable ResultSet rs) { - if (rs != null) { - try { - rs.close(); - } catch (SQLException e) { - LoggerFactory.getLogger(MeasureFilterSql.class).warn("Fail to close result set", e); - // ignore - } - } - if (stmt != null) { - try { - stmt.close(); - } catch (SQLException e) { - LoggerFactory.getLogger(MeasureFilterSql.class).warn("Fail to close statement", e); - // ignore - } - } - - } } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java index 240717336ff..89c4dff46ff 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java @@ -19,6 +19,15 @@ */ 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 */ @@ -78,4 +87,37 @@ public final class DatabaseUtils { "user_roles", "widgets", "widget_properties"}; + + 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 + } + } + } } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java b/sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java index d12f3fa1e49..4812f7c8b18 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java @@ -37,6 +37,7 @@ import org.sonar.jpa.session.CustomHibernateConnectionProvider; import javax.sql.DataSource; +import java.sql.Connection; import java.sql.SQLException; import java.util.List; import java.util.Map; @@ -64,6 +65,7 @@ public class DefaultDatabase implements Database { try { initSettings(); initDatasource(); + checkConnection(); return this; } catch (Exception e) { @@ -116,12 +118,25 @@ public class DefaultDatabase implements Database { private void initDatasource() throws Exception {// NOSONAR this exception is thrown by BasicDataSourceFactory // but it's correctly caught by start() - LOG.info("Create JDBC datasource to url " + properties.getProperty(DatabaseProperties.PROP_URL, DEFAULT_URL)); + LOG.info("Create JDBC datasource for " + properties.getProperty(DatabaseProperties.PROP_URL, DEFAULT_URL)); datasource = (BasicDataSource) BasicDataSourceFactory.createDataSource(extractCommonsDbcpProperties(properties)); datasource.setConnectionInitSqls(dialect.getConnectionInitStatements(getSchema())); datasource.setValidationQuery(dialect.getValidationQuery()); } + private void checkConnection() { + Connection connection = null; + try { + LOG.debug("Testing JDBC connection"); + connection = datasource.getConnection(); + } catch (Exception e) { + LOG.error("Can not connect to database. Please check connectivity and settings (see the properties prefixed by 'sonar.jdbc.').", e); + } finally { + DatabaseUtils.closeQuietly(connection); + } + } + + public final DefaultDatabase stop() { if (datasource != null) { try { -- 2.39.5