]> source.dussan.org Git - sonarqube.git/commitdiff
Check db connection before Rails initialization in order to improve logs
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 18 Oct 2012 09:06:44 +0000 (11:06 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 18 Oct 2012 09:06:44 +0000 (11:06 +0200)
sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java
sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java
sonar-core/src/main/java/org/sonar/core/persistence/DatabaseUtils.java
sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java

index ebf5cec28099d0cf6ebaae59919364b8834d53cd..6aac5b504a80eeff7fb3c20afa118798e7b5f25c 100644 (file)
@@ -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));
index 68015b1e07e45a950966841db0dd6fa407660209..176d50f8e41d6bd39f62aa2b62fced55726e1fe8 100644 (file)
@@ -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
-      }
-    }
-
-  }
 }
index 240717336ff0c12216b3c8c22e07afe8eb4d5ede..89c4dff46ff7cb4d1859d5a1f0b70a5e1f1a88fa 100644 (file)
  */
 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
+      }
+    }
+  }
 }
index d12f3fa1e4986b7bc27b5205f8b628b138d546c1..4812f7c8b187f79ede9e35809aa936403a607ece 100644 (file)
@@ -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 {