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;
} 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));
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;
return process(rs);
} finally {
- closeQuietly(statement, rs);
+ DatabaseUtils.closeQuietly(rs);
+ DatabaseUtils.closeQuietly(statement);
}
}
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
- }
- }
-
- }
}
*/
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
*/
"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
+ }
+ }
+ }
}
import javax.sql.DataSource;
+import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
try {
initSettings();
initDatasource();
+ checkConnection();
return this;
} catch (Exception e) {
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 {