]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2965 enforce validation query on DataSource
authorDavid Gageot <david@gageot.net>
Wed, 11 Jul 2012 11:48:59 +0000 (13:48 +0200)
committerDavid Gageot <david@gageot.net>
Wed, 11 Jul 2012 15:38:40 +0000 (17:38 +0200)
sonar-application/src/main/assembly/conf/sonar.properties
sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/Dialect.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/H2.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/MsSql.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/MySql.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/Oracle.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSql.java
sonar-core/src/test/java/org/sonar/core/persistence/H2Database.java
sonar-server/src/main/webapp/WEB-INF/app/models/server.rb

index e48cda0d6d52396f751f58c2c5e8b01acc26717d..acdaba0433cee3954b0325cdbac4513190a924d3 100644 (file)
@@ -45,7 +45,6 @@ sonar.jdbc.password:                       sonar
 # Comment the following line to deactivate the default embedded database.
 sonar.jdbc.url:                            jdbc:h2:tcp://localhost:9092/sonar
 #sonar.jdbc.driverClassName:                org.h2.Driver
-#sonar.jdbc.validationQuery:                values(1)
 
 # directory containing H2 database files. By default it's the /data directory in the sonar installation.
 #sonar.embeddedDatabase.dataDir:
@@ -59,7 +58,6 @@ sonar.jdbc.url:                            jdbc:h2:tcp://localhost:9092/sonar
 
 # Optional properties
 #sonar.jdbc.driverClassName:                com.mysql.jdbc.Driver
-#sonar.jdbc.validationQuery:                select 1
 
 
 #----- Oracle 10g/11g
@@ -73,7 +71,6 @@ sonar.jdbc.url:                            jdbc:h2:tcp://localhost:9092/sonar
 
 # Optional properties
 #sonar.jdbc.driverClassName:                oracle.jdbc.OracleDriver
-#sonar.jdbc.validationQuery:                select 1 from dual
 
 # Uncomment the following property if the Oracle account has permissions to access multiple schemas,
 # for example sonar schemas with different versions. In that case, use the same property during project analysis
@@ -87,7 +84,6 @@ sonar.jdbc.url:                            jdbc:h2:tcp://localhost:9092/sonar
 
 # Optional properties
 #sonar.jdbc.driverClassName:                org.postgresql.Driver
-#sonar.jdbc.validationQuery:                select 1
 
 # Uncomment the following property if the PostgreSQL account has permissions to access multiple schemas,
 # for example sonar schemas with different versions. In that case, use the same property during project analysis
@@ -101,7 +97,6 @@ sonar.jdbc.url:                            jdbc:h2:tcp://localhost:9092/sonar
 
 # Optional properties
 #sonar.jdbc.driverClassName:                net.sourceforge.jtds.jdbc.Driver
-#sonar.jdbc.validationQuery:                select 1  
 
 
 #----- Connection pool settings
index cf3b54f99f08607fe4b3541716c93df4f4853619..8e4b7f4625af4a6784f02b880672b71cae89efd9 100644 (file)
@@ -124,6 +124,8 @@ public class DefaultDatabase implements Database {
     if (StringUtils.isNotBlank(initStatement)) {
       datasource.setConnectionInitSqls(Arrays.asList(initStatement));
     }
+
+    datasource.setValidationQuery(dialect.getValidationQuery());
   }
 
   public final DefaultDatabase stop() {
index 03f368620d61b4c5c900b76c55c72ba481298243..35a26c4b42b618e017e9dc61814d5d564d9629ec 100644 (file)
@@ -68,4 +68,11 @@ public interface Dialect {
    * @since 2.14
    */
   String getFalseSqlValue();
+
+  /**
+   * Query used to validate the jdbc connection.
+   * 
+   * @since 3.2
+   */
+  String getValidationQuery();
 }
index 00879be648b8af2297fcce981c56d52d129d92c6..fd761f82d244fcfc00e3c9621b0f468d3412b2e7 100644 (file)
@@ -64,4 +64,8 @@ public class H2 implements Dialect {
   public String getFalseSqlValue() {
     return "false";
   }
+
+  public String getValidationQuery() {
+    return "SELECT 1";
+  }
 }
index 14589780d837b3ef01dff7088807911ea19b1437..0b0e944f489bccf6a0b9db336735b6fc67269402 100644 (file)
@@ -85,5 +85,9 @@ public class MsSql implements Dialect {
   public String getFalseSqlValue() {
     return "0";
   }
+
+  public String getValidationQuery() {
+    return "SELECT 1";
+  }
 }
 
index 0f718bdb710aa58b517c48f46621a00db98ea8f0..94f8dc8cf1ac2cafd695e3cfeb2784947eb3ed5c 100644 (file)
@@ -77,4 +77,8 @@ public class MySql implements Dialect {
   public String getFalseSqlValue() {
     return "false";
   }
+
+  public String getValidationQuery() {
+    return "SELECT 1";
+  }
 }
index 6adbb43e52143c275d2ed91190a436e26df85f1f..a9f235a23e85b658b451b900b3970f2bc96e7e61 100644 (file)
@@ -84,4 +84,8 @@ public class Oracle implements Dialect {
   public String getFalseSqlValue() {
     return "0";
   }
+
+  public String getValidationQuery() {
+    return "SELECT 1 FROM DUAL";
+  }
 }
index 8c1b29cc3c538948f3783ff9ed85b73b98b07150..07d5ddd48edd8f55a94235b6cd4d6aee2c772aaf 100644 (file)
@@ -81,4 +81,8 @@ public class PostgreSql implements Dialect {
   public String getFalseSqlValue() {
     return "false";
   }
+
+  public String getValidationQuery() {
+    return "SELECT 1";
+  }
 }
index 55db7c2ed942928be05c59faa2cca5a26c069cd4..3767121de0dc42ab19a2937c7a23c11f3f2e128e 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.core.persistence;
 
 import org.apache.commons.dbcp.BasicDataSource;
-import org.apache.commons.dbcp.BasicDataSourceFactory;
 import org.hibernate.cfg.Environment;
 import org.sonar.core.persistence.dialect.Dialect;
 import org.sonar.core.persistence.dialect.H2;
@@ -53,18 +52,13 @@ public class H2Database implements Database {
    */
   private void startDatabase() {
     try {
-      Properties properties = new Properties();
-      properties.put("driverClassName", "org.h2.Driver");
-      properties.put("username", "sonar");
-      properties.put("password", "sonar");
-      // properties.put("url", "jdbc:h2:mem:sonar2;TRACE_LEVEL_SYSTEM_OUT=2");
-      properties.put("url", "jdbc:h2:mem:sonar2");
-
-      // limit to 2 because of Hibernate and MyBatis
-      properties.put("maxActive", "2");
-      properties.put("maxIdle", "2");
-      datasource = (BasicDataSource) BasicDataSourceFactory.createDataSource(properties);
-
+      datasource = new BasicDataSource();
+      datasource.setDriverClassName("org.h2.Driver");
+      datasource.setUsername("sonar");
+      datasource.setPassword("sonar");
+      datasource.setUrl("jdbc:h2:mem:sonar2");
+      datasource.setMaxActive(2);
+      datasource.setMaxIdle(2);
     } catch (Exception e) {
       throw new IllegalStateException("Fail to start H2", e);
     }
index 7541819bee4c45fb2628b64078b364f8ebf419c5..d2e50aafbac992b00c160571cf34e6a08c2785c8 100644 (file)
@@ -68,7 +68,6 @@ class Server
     add_property(sonar_info, 'Database Driver') {"#{jdbc_metadata.getDriverName()} #{jdbc_metadata.getDriverVersion()}"}
     add_property(sonar_info, 'Database Driver Class') {sonar_property('sonar.jdbc.driverClassName')}
     add_property(sonar_info, 'Database Dialect (Hibernate)') {"#{Java::OrgSonarServerUi::JRubyFacade.getInstance().getDatabase().getDialect().getId()} (#{Java::OrgSonarServerUi::JRubyFacade.getInstance().getDatabase().getDialect().getHibernateDialectClass().getName()})"}
-    add_property(sonar_info, 'Database Validation Query') {sonar_property('sonar.jdbc.validationQuery')}
     add_property(sonar_info, 'Hibernate Default Schema') {sonar_property('sonar.hibernate.default_schema')}
     add_property(sonar_info, 'External User Authentication') {sonar_property(org.sonar.api.CoreProperties.CORE_AUTHENTICATOR_CLASS)}
     add_property(sonar_info, 'Automatic User Creation') {sonar_property(org.sonar.api.CoreProperties.CORE_AUTHENTICATOR_CREATE_USERS)}