[PATCH] Added a validation SQL query to database backends

Wald Commits scm-commit at wald.intevation.org
Wed Nov 12 13:31:57 CET 2014


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1415795513 -3600
# Node ID c5a7aae523966f7f89a0ea68babbace6c9e04416
# Parent  07914728265025448da4ce8c78cabd7e5e5acf41
Added a validation SQL query to database backends.
Enable it by storing something like "SELECT 1 FROM DUAL" in config
under /artifact-database/backend-database/validation-query/text()
or /artifact-database/seddb-database/validation-query/text().

diff -r 079147282650 -r c5a7aae52396 backend/src/main/java/org/dive4elements/river/backend/Credentials.java
--- a/backend/src/main/java/org/dive4elements/river/backend/Credentials.java	Thu Nov 06 15:16:37 2014 +0100
+++ b/backend/src/main/java/org/dive4elements/river/backend/Credentials.java	Wed Nov 12 13:31:53 2014 +0100
@@ -16,6 +16,7 @@
     protected String   driver;
     protected String   url;
     protected String   connectionInitSqls;
+    protected String   validationQuery;
     protected Class [] classes;
 
     public Credentials() {
@@ -28,6 +29,7 @@
         String   driver,
         String   url,
         String   connectionInitSqls,
+        String   validationQuery,
         Class [] classes
     ) {
         this.user               = user;
@@ -36,6 +38,7 @@
         this.driver             = driver;
         this.url                = url;
         this.connectionInitSqls = connectionInitSqls;
+        this.validationQuery    = validationQuery;
         this.classes            = classes;
     }
 
@@ -79,6 +82,14 @@
         this.url = url;
     }
 
+    public String getValidationQuery() {
+        return validationQuery;
+    }
+
+    public void setValidationQuery(String validationQuery) {
+        this.validationQuery = validationQuery;
+    }
+
     public String getConnectionInitSqls() {
         return connectionInitSqls;
     }
diff -r 079147282650 -r c5a7aae52396 backend/src/main/java/org/dive4elements/river/backend/FLYSCredentials.java
--- a/backend/src/main/java/org/dive4elements/river/backend/FLYSCredentials.java	Thu Nov 06 15:16:37 2014 +0100
+++ b/backend/src/main/java/org/dive4elements/river/backend/FLYSCredentials.java	Wed Nov 12 13:31:53 2014 +0100
@@ -108,6 +108,9 @@
     public static final String XPATH_CONNECTION_INIT_SQLS =
         "/artifact-database/backend-database/connection-init-sqls/text()";
 
+    public static final String XPATH_VALIDATION_QUERY =
+        "/artifact-database/backend-database/validation-query/text()";
+
     public static final String DEFAULT_USER =
         System.getProperty("flys.backend.user", "flys");
 
@@ -133,6 +136,10 @@
         System.getProperty(
             "flys.backend.connection.init.sqls");
 
+    public static final String DEFAULT_VALIDATION_QUERY =
+        System.getProperty(
+            "flys.backend.connection.validation.query");
+
     public static final Class [] CLASSES = {
         Annotation.class,
         AnnotationType.class,
@@ -221,10 +228,12 @@
         String dialect,
         String driver,
         String url,
-        String connectionInitSqls
+        String connectionInitSqls,
+        String validationQuery
     ) {
         super(
-            user, password, dialect, driver, url, connectionInitSqls, CLASSES);
+            user, password, dialect, driver, url,
+            connectionInitSqls, validationQuery, CLASSES);
     }
 
     private static Credentials instance;
@@ -245,9 +254,13 @@
                 Config.getStringXPath(
                     XPATH_CONNECTION_INIT_SQLS,
                     DEFAULT_CONNECTION_INIT_SQLS);
+            String validationQuery =
+                Config.getStringXPath(
+                    XPATH_VALIDATION_QUERY,
+                    DEFAULT_VALIDATION_QUERY);
 
             instance = new FLYSCredentials(
-                user, password, dialect, driver, url, connectionInitSqls);
+                user, password, dialect, driver, url, connectionInitSqls, validationQuery);
         }
         return instance;
     }
@@ -259,7 +272,8 @@
             DEFAULT_DIALECT,
             DEFAULT_DRIVER,
             DEFAULT_URL,
-            DEFAULT_CONNECTION_INIT_SQLS);
+            DEFAULT_CONNECTION_INIT_SQLS,
+            DEFAULT_VALIDATION_QUERY);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r 079147282650 -r c5a7aae52396 backend/src/main/java/org/dive4elements/river/backend/SedDBCredentials.java
--- a/backend/src/main/java/org/dive4elements/river/backend/SedDBCredentials.java	Thu Nov 06 15:16:37 2014 +0100
+++ b/backend/src/main/java/org/dive4elements/river/backend/SedDBCredentials.java	Wed Nov 12 13:31:53 2014 +0100
@@ -84,6 +84,9 @@
     public static final String XPATH_CONNECTION_INIT_SQLS =
         "/artifact-database/seddb-database/connection-init-sqls/text()";
 
+    public static final String XPATH_VALIDATION_QUERY =
+        "/artifact-database/seddb-database/validation-query/text()";
+
     public static final String DEFAULT_USER =
         System.getProperty("flys.seddb.user", "seddb");
 
@@ -109,6 +112,10 @@
         System.getProperty(
             "flys.seddb.connection.init.sqls");
 
+    public static final String DEFAULT_VALIDATION_QUERY =
+        System.getProperty(
+            "flys.seddb.connection.validation.query");
+
     public static final Class [] CLASSES = {
         BezugspegelgewId.class,
         Bezugspegelgew.class,
@@ -175,10 +182,12 @@
         String dialect,
         String driver,
         String url,
-        String connectionInitSqls
+        String connectionInitSqls,
+        String validationQuery
     ) {
         super(
-            user, password, dialect, driver, url, connectionInitSqls, CLASSES);
+            user, password, dialect, driver, url,
+            connectionInitSqls, validationQuery, CLASSES);
     }
 
     public static synchronized Credentials getInstance() {
@@ -197,9 +206,14 @@
                 Config.getStringXPath(
                     XPATH_CONNECTION_INIT_SQLS,
                     DEFAULT_CONNECTION_INIT_SQLS);
+            String validationQuery =
+                Config.getStringXPath(
+                    XPATH_VALIDATION_QUERY,
+                    DEFAULT_VALIDATION_QUERY);
 
             instance = new SedDBCredentials(
-                user, password, dialect, driver, url, connectionInitSqls);
+                user, password, dialect, driver, url,
+                connectionInitSqls, validationQuery);
         }
         return instance;
     }
@@ -211,7 +225,8 @@
             DEFAULT_DIALECT,
             DEFAULT_DRIVER,
             DEFAULT_URL,
-            DEFAULT_CONNECTION_INIT_SQLS);
+            DEFAULT_CONNECTION_INIT_SQLS,
+            DEFAULT_VALIDATION_QUERY);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r 079147282650 -r c5a7aae52396 backend/src/main/java/org/dive4elements/river/backend/SessionFactoryProvider.java
--- a/backend/src/main/java/org/dive4elements/river/backend/SessionFactoryProvider.java	Thu Nov 06 15:16:37 2014 +0100
+++ b/backend/src/main/java/org/dive4elements/river/backend/SessionFactoryProvider.java	Wed Nov 12 13:31:53 2014 +0100
@@ -144,6 +144,10 @@
         if (connectionInitSqls != null) {
             props.setProperty("connectionInitSqls", connectionInitSqls);
         }
+        String validationQuery = credentials.getValidationQuery();
+        if (validationQuery != null) {
+            props.setProperty("validationQuery", validationQuery);
+        }
 
         cfg.mergeProperties(props);
 
diff -r 079147282650 -r c5a7aae52396 backend/src/main/java/org/dive4elements/river/backend/utils/DBCPConnectionProvider.java
--- a/backend/src/main/java/org/dive4elements/river/backend/utils/DBCPConnectionProvider.java	Thu Nov 06 15:16:37 2014 +0100
+++ b/backend/src/main/java/org/dive4elements/river/backend/utils/DBCPConnectionProvider.java	Wed Nov 12 13:31:53 2014 +0100
@@ -211,6 +211,11 @@
                 StringTokenizer tokenizer = new StringTokenizer(connectionInitSqls, ";");
                 ds.setConnectionInitSqls(Collections.list(tokenizer));
             }
+
+            String validationQuery = props.getProperty("validationQuery");
+            if (validationQuery != null) {
+                ds.setValidationQuery(validationQuery);
+            }
             // The BasicDataSource has lazy initialization
             // borrowing a connection will start the DataSource
             // and make sure it is configured correctly.


More information about the Dive4Elements-commits mailing list