fixed sonar issue in JdbcCachedDataSource 13/75413/3
authorRich Tabedzki <richard.tabedzki@att.com>
Mon, 7 Jan 2019 20:10:11 +0000 (15:10 -0500)
committerRich Tabedzki <richard.tabedzki@att.com>
Mon, 7 Jan 2019 20:31:41 +0000 (15:31 -0500)
Changes made:
* updated cleanup code
* added TerminatingConfiguration class

Change-Id: Iee16116a5fc419065915b9f84488874dd192af7c
Issue-ID: CCSDK-913
Signed-off-by: Rich Tabedzki <richard.tabedzki@att.com>
dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/CachedDataSource.java
dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/TerminatingCachedDataSource.java
dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/BaseDBConfiguration.java
dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/TerminatingConfiguration.java [new file with mode: 0755]
dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDBCachedDataSource.java

index f3ecfa2..0f1674f 100755 (executable)
@@ -94,6 +94,7 @@ public abstract class CachedDataSource implements DataSource, SQLExecutionMonito
     }
 
     protected abstract DataSource configure(BaseDBConfiguration jdbcElem) throws DBConfigException;
+    protected abstract int getAvailableConnections();
 
     /*
      * (non-Javadoc)
@@ -488,9 +489,9 @@ public abstract class CachedDataSource implements DataSource, SQLExecutionMonito
             isSlave = true;
         }
         if (isSlave) {
-            LOGGER.debug("SQL SLAVE : {} on server {}", connectionName, hostname);
+            LOGGER.debug("SQL SLAVE : {} on server {}, pool {}", connectionName, getDbConnectionName(), getAvailableConnections());
         } else {
-            LOGGER.debug("SQL MASTER : {} on server {}", connectionName, hostname);
+            LOGGER.debug("SQL MASTER : {} on server {}, pool {}", connectionName, getDbConnectionName(), getAvailableConnections());
         }
         return isSlave;
     }
index e1afcc2..884f888 100755 (executable)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -29,6 +29,8 @@ import org.onap.ccsdk.sli.core.dblib.pm.SQLExecutionMonitorObserver;
 
 public class TerminatingCachedDataSource extends CachedDataSource implements SQLExecutionMonitorObserver {
 
+    private static final int DEFAULT_AVAILABLE_CONNECTIONS = 0;
+
     public TerminatingCachedDataSource(BaseDBConfiguration jdbcElem) throws DBConfigException {
         super(jdbcElem);
     }
@@ -43,4 +45,8 @@ public class TerminatingCachedDataSource extends CachedDataSource implements SQL
         return null;
     }
 
+    @Override
+    protected int getAvailableConnections() {
+        return DEFAULT_AVAILABLE_CONNECTIONS;
+    }
 }
index 967059a..ea6bc45 100755 (executable)
@@ -29,7 +29,7 @@ import org.slf4j.LoggerFactory;
  */
 public abstract class BaseDBConfiguration {
 
-    private Logger logger = LoggerFactory.getLogger(BaseDBConfiguration.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(BaseDBConfiguration.class);
     /**
      * Property key within a properties configuration File for db type
      */
@@ -134,7 +134,7 @@ public abstract class BaseDBConfiguration {
             String value = properties.getProperty(CONNECTION_TIMEOUT, DEFAULT_REJECT_CHANGE_VALUE);
             return Integer.parseInt(value);
         } catch (Exception exc) {
-            logger.error("Exception",exc);
+            LOGGER.error("Exception",exc);
             return Integer.parseInt(DEFAULT_REJECT_CHANGE_VALUE);
         }
     }
@@ -150,7 +150,7 @@ public abstract class BaseDBConfiguration {
             String value = properties.getProperty(REQUEST_TIMEOUT, DEFAULT_REJECT_CHANGE_VALUE);
             return Integer.parseInt(value);
         } catch (Exception exc) {
-            logger.error("Exception",exc);
+            LOGGER.error("Exception",exc);
             return Integer.parseInt(DEFAULT_REJECT_CHANGE_VALUE);
         }
     }
@@ -247,4 +247,12 @@ public abstract class BaseDBConfiguration {
     public String getDbUrl() {
         return properties.getProperty(DATABASE_URL);
     }
+    
+    public boolean containsKey(String propertyname) {
+        return properties.containsKey(propertyname);
+    }
+    
+    public String getProperty(String propertyname) {
+        return properties.getProperty(propertyname);
+    }
 }
diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/TerminatingConfiguration.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/TerminatingConfiguration.java
new file mode 100755 (executable)
index 0000000..1ebd144
--- /dev/null
@@ -0,0 +1,33 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * onap
+ * ================================================================================
+ * Copyright (C) 2016 - 2017 ONAP
+ * ================================================================================
+ * Modifications Copyright (C) 2018 IBM.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.ccsdk.sli.core.dblib.config;
+
+import java.util.Properties;
+
+public class TerminatingConfiguration extends BaseDBConfiguration {
+
+    public TerminatingConfiguration() {
+        super(new Properties());
+    }
+
+}
index 09c1c20..a53d186 100755 (executable)
@@ -193,4 +193,9 @@ public class JdbcDBCachedDataSource extends CachedDataSource {
         dataSource.close(true);
         super.cleanUp();
     }
+
+    @Override
+    protected int getAvailableConnections() {
+        return org.apache.tomcat.jdbc.pool.DataSource.class.cast(ds).getSize();
+    }
 }