From d3913648518ce15fe7a74275394b86d0ecfb32a4 Mon Sep 17 00:00:00 2001 From: Rich Tabedzki Date: Fri, 15 Sep 2017 01:02:37 +0000 Subject: Prepared DBLIB for multi-verndor JDBC Driver Changes made: 1. removed hardcoded JDBC driver's name and made it a property. 2. Optimized connection pool initialization 3. Removed unused factories. Change-Id: Id1d1868f7f1a6ae5a0429a97403042a8f6ee78d3 Issue-ID: CCSDK-92 Signed-off-by: Rich Tabedzki --- .../ccsdk/sli/core/dblib/CachedDataSource.java | 5 +- .../ccsdk/sli/core/dblib/DBResourceManager.java | 169 +++++++++++++++---- .../sli/core/dblib/config/BaseDBConfiguration.java | 126 +++++++------- .../factory/AbstractDBResourceManagerFactory.java | 39 ----- .../factory/AbstractResourceManagerFactory.java | 104 ------------ .../dblib/factory/FactoryNotDefinedException.java | 37 ----- .../core/dblib/jdbc/JdbcDBCachedDataSource.java | 46 +----- .../dblib/jdbc/JdbcDbResourceManagerFactory.java | 183 --------------------- 8 files changed, 210 insertions(+), 499 deletions(-) mode change 100644 => 100755 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/CachedDataSource.java mode change 100644 => 100755 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java mode change 100644 => 100755 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/BaseDBConfiguration.java delete mode 100644 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/AbstractDBResourceManagerFactory.java delete mode 100644 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/AbstractResourceManagerFactory.java delete mode 100644 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/FactoryNotDefinedException.java mode change 100644 => 100755 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDBCachedDataSource.java delete mode 100644 dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDbResourceManagerFactory.java diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/CachedDataSource.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/CachedDataSource.java old mode 100644 new mode 100755 index ee8ab2f3..a5902d54 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/CachedDataSource.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/CachedDataSource.java @@ -20,7 +20,6 @@ package org.onap.ccsdk.sli.core.dblib; -import com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException; import org.apache.tomcat.jdbc.pool.PoolExhaustedException; import org.onap.ccsdk.sli.core.dblib.config.BaseDBConfiguration; import org.onap.ccsdk.sli.core.dblib.pm.SQLExecutionMonitor; @@ -535,7 +534,7 @@ public abstract class CachedDataSource implements DataSource, SQLExecutionMonito return monitor; } - protected boolean isSlave() throws PoolExhaustedException, MySQLNonTransientConnectionException { + protected boolean isSlave() throws PoolExhaustedException { CachedRowSet rs = null; boolean isSlave = true; String hostname = "UNDETERMINED"; @@ -547,7 +546,7 @@ public abstract class CachedDataSource implements DataSource, SQLExecutionMonito hostname = rs.getString(2); } isSlave = localSlave; - } catch(PoolExhaustedException | MySQLNonTransientConnectionException peexc){ + } catch(PoolExhaustedException peexc){ throw peexc; } catch (SQLException e) { LOGGER.error("", e); diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java old mode 100644 new mode 100755 index 7a27a20c..a2eb0f9c --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBResourceManager.java @@ -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. @@ -20,11 +20,8 @@ package org.onap.ccsdk.sli.core.dblib; -import com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException; import org.apache.tomcat.jdbc.pool.PoolExhaustedException; import org.onap.ccsdk.sli.core.dblib.config.DbConfigPool; -import org.onap.ccsdk.sli.core.dblib.factory.AbstractDBResourceManagerFactory; -import org.onap.ccsdk.sli.core.dblib.factory.AbstractResourceManagerFactory; import org.onap.ccsdk.sli.core.dblib.factory.DBConfigFactory; import org.onap.ccsdk.sli.core.dblib.pm.PollingWorker; import org.onap.ccsdk.sli.core.dblib.pm.SQLExecutionMonitor; @@ -52,8 +49,21 @@ import java.util.Properties; import java.util.Queue; import java.util.Set; import java.util.TimerTask; +import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.atomic.AtomicBoolean; +import javax.sql.DataSource; +import javax.sql.rowset.CachedRowSet; + +import org.apache.tomcat.jdbc.pool.PoolExhaustedException; +import org.onap.ccsdk.sli.core.dblib.config.DbConfigPool; +import org.onap.ccsdk.sli.core.dblib.config.JDBCConfiguration; +import org.onap.ccsdk.sli.core.dblib.factory.DBConfigFactory; +import org.onap.ccsdk.sli.core.dblib.pm.PollingWorker; +import org.onap.ccsdk.sli.core.dblib.pm.SQLExecutionMonitor; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @version $Revision: 1.15 $ @@ -72,11 +82,18 @@ public class DBResourceManager implements DataSource, DataAccessor, DBResourceOb protected final AtomicBoolean dsSelector = new AtomicBoolean(); // Queue dsQueue = new ConcurrentLinkedQueue(); - Queue dsQueue = new PriorityQueue<>(4, new Comparator() { + Queue dsQueue = new PriorityQueue(4, new Comparator() { @Override public int compare(CachedDataSource left, CachedDataSource right) { try { - if (!left.isSlave()) { + if(left == null){ + return 1; + } + if(right == null){ + return -1; + } + + if(!left.isSlave()) { return -1; } if (!right.isSlave()) { @@ -144,31 +161,108 @@ public class DBResourceManager implements DataSource, DataAccessor, DBResourceOb } private void config(Properties configProps) throws Exception { - + final ConcurrentLinkedQueue semaphore = new ConcurrentLinkedQueue(); final DbConfigPool dbConfig = DBConfigFactory.createConfig(configProps); - final AbstractResourceManagerFactory factory = - AbstractDBResourceManagerFactory.getFactory(dbConfig.getType()); - LOGGER.info("Default DB config is : {}", dbConfig.getType()); - LOGGER.info("Using factory : {}", factory.getClass().getName()); - final CachedDataSource[] cachedDS = factory.initDBResourceManager(dbConfig, this); - if (cachedDS == null || cachedDS.length == 0) { - LOGGER.error("Initialization of CachedDataSources failed. No instance was created."); - throw new Exception("Failed to initialize DB Library. No data source was created."); + long startTime = System.currentTimeMillis(); + + try { + JDBCConfiguration[] config = dbConfig.getJDBCbSourceArray(); + CachedDataSource[] cachedDS = new CachedDataSource[config.length]; + if (cachedDS == null || cachedDS.length == 0) { + LOGGER.error("Initialization of CachedDataSources failed. No instance was created."); + throw new Exception("Failed to initialize DB Library. No data source was created."); + } + + for(int i = 0; i < config.length; i++) { + cachedDS[i] = CachedDataSourceFactory.createDataSource(config[i]); + if(cachedDS[i] == null) + continue; + semaphore.add(cachedDS[i]); + cachedDS[i].setInterval(monitoringInterval); + cachedDS[i].setInitialDelay(monitoringInitialDelay); + cachedDS[i].setExpectedCompletionTime(expectedCompletionTime); + cachedDS[i].setUnprocessedFailoverThreshold(unprocessedFailoverThreshold); + cachedDS[i].addObserver(DBResourceManager.this); + } + +// CachedDataSource[] cachedDS = factory.initDBResourceManager(dbConfig, DBResourceManager.this, semaphore); + DataSourceTester[] tester = new DataSourceTester[config.length]; + + for(int i=0; i semaphoreQ; + + public DataSourceTester(CachedDataSource ds, DBResourceManager manager, ConcurrentLinkedQueue semaphore) { + this.ds = ds; + this.manager = manager; + this.semaphoreQ = semaphore; } - for (final CachedDataSource ds : cachedDS) { - if(ds != null && ds.isInitialized()){ - setDataSource(ds); - ds.setInterval(monitoringInterval); - ds.setInitialDelay(monitoringInitialDelay); - ds.setExpectedCompletionTime(expectedCompletionTime); - ds.setUnprocessedFailoverThreshold(unprocessedFailoverThreshold); - ds.addObserver(this); + @Override + public void run() { + manager.setDataSource(ds); + boolean slave = true; + if(ds != null) { + try { + slave = ds.isSlave(); + } catch (Exception exc) { + LOGGER.warn("", exc); + } } + if(!slave) { + LOGGER.info(String.format("Adding MASTER (%s) to active queue", ds.getDbConnectionName())); + try { + synchronized (semaphoreQ) { + semaphoreQ.notifyAll(); + } + } catch(Exception exc) { + LOGGER.warn("", exc); + } } + try { + synchronized (semaphoreQ) { + semaphoreQ.remove(ds); + } + if(semaphoreQ.isEmpty()) { + synchronized (semaphoreQ) { + semaphoreQ.notifyAll(); + } + } + } catch(Exception exc) { + LOGGER.warn("", exc); + } + LOGGER.info(String.format("Thread DataSourceTester terminated %s for %s", this.getName(), ds.getDbConnectionName())); + } + } + private long getLongFromProperties(Properties props, String property, long defaultValue) { String value = null; @@ -306,7 +400,7 @@ public class DBResourceManager implements DataSource, DataAccessor, DBResourceOb CachedDataSource active = null; // test if there are any connection pools available - LinkedList sources = new LinkedList<>(this.dsQueue); + LinkedList sources = new LinkedList(this.dsQueue); if(sources.isEmpty()){ LOGGER.error("Generated alarm: DBResourceManager.getData - No active DB connection pools are available."); throw new DBLibException("No active DB connection pools are available in RequestDataWithRecovery call."); @@ -423,8 +517,8 @@ public class DBResourceManager implements DataSource, DataAccessor, DBResourceOb return writeDataNoRecovery(statement, newList, preferredDS); } - CachedDataSource findMaster() throws PoolExhaustedException, MySQLNonTransientConnectionException { - CachedDataSource master; + CachedDataSource findMaster() throws PoolExhaustedException { + CachedDataSource master = null; CachedDataSource[] dss = this.dsQueue.toArray(new CachedDataSource[0]); for(int i=0; i list = new ArrayList<>(); + ArrayList list = new ArrayList(); list.addAll(dsQueue); list.addAll(broken); if (htmlFormat) @@ -785,7 +880,7 @@ public class DBResourceManager implements DataSource, DataAccessor, DBResourceOb public String getPreferredDataSourceName(AtomicBoolean flipper) { - LinkedList snapshot = new LinkedList<>(dsQueue); + LinkedList snapshot = new LinkedList(dsQueue); if(snapshot.size() > 1){ CachedDataSource first = snapshot.getFirst(); CachedDataSource last = snapshot.getLast(); @@ -849,4 +944,8 @@ public class DBResourceManager implements DataSource, DataAccessor, DBResourceOb ds.getPoolInfo(false); } } + + public int poolSize() { + return dsQueue.size(); + } } diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/BaseDBConfiguration.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/BaseDBConfiguration.java old mode 100644 new mode 100755 index d53be0e7..a8bc58b8 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/BaseDBConfiguration.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/config/BaseDBConfiguration.java @@ -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. @@ -30,58 +30,68 @@ public abstract class BaseDBConfiguration { /** * Property key within a properties configuration File for db type */ - public static final String DATABASE_TYPE = "org.onap.ccsdk.sli.dbtype"; + public static final String DATABASE_TYPE = "org.onap.ccsdk.sli.dbtype"; /** * Property key with a properties configuration File for db url */ - public static final String DATABASE_URL = "org.onap.ccsdk.sli.jdbc.url"; + public static final String DATABASE_URL = "org.onap.ccsdk.sli.jdbc.url"; /** * Property key with a properties configuration File for database name */ - public static final String DATABASE_NAME = "org.onap.ccsdk.sli.jdbc.database"; + public static final String DATABASE_NAME = "org.onap.ccsdk.sli.jdbc.database"; + + /** + * Property key with a properties configuration File for jdbc driver + */ + public static final String DRIVER_NAME = "org.onap.ccsdk.sli.jdbc.driver"; /** * Property key with a properties configuration File for db database connection name */ - public static final String CONNECTION_NAME = "org.onap.ccsdk.sli.jdbc.connection.name"; + public static final String CONNECTION_NAME = "org.onap.ccsdk.sli.jdbc.connection.name"; /** * Property key with a properties configuration File for database user */ - public static final String DATABASE_USER = "org.onap.ccsdk.sli.jdbc.user"; + public static final String DATABASE_USER = "org.onap.ccsdk.sli.jdbc.user"; /** - * Property key with a properties configuration File for database password for associated with - * org.onap.ccsdk.sli.jdbc.user. + * Property key with a properties configuration File for database password + * for associated with org.onap.ccsdk.sli.jdbc.user. */ - public static final String DATABASE_PSSWD = "org.onap.ccsdk.sli.jdbc.password"; + public static final String DATABASE_PSSWD = "org.onap.ccsdk.sli.jdbc.password"; /** - * Property key with a properties configuration File for database connection timeout + * Property key with a properties configuration File for database connection + * timeout */ public static final String CONNECTION_TIMEOUT="org.onap.ccsdk.sli.jdbc.connection.timeout"; /** - * Property key with a properties configuration File for database request timeout + * Property key with a properties configuration File for database request + * timeout */ - public static final String REQUEST_TIMEOUT = "org.onap.ccsdk.sli.jdbc.request.timeout"; + public static final String REQUEST_TIMEOUT = "org.onap.ccsdk.sli.jdbc.request.timeout"; /** - * Property key with a properties configuration File for database minimum limit + * Property key with a properties configuration File for database minimum + * limit */ - public static final String MIN_LIMIT = "org.onap.ccsdk.sli.jdbc.limit.min"; + public static final String MIN_LIMIT = "org.onap.ccsdk.sli.jdbc.limit.min"; /** - * Property key with a properties configuration File for database maximum limit + * Property key with a properties configuration File for database maximum + * limit */ - public static final String MAX_LIMIT = "org.onap.ccsdk.sli.jdbc.limit.max"; + public static final String MAX_LIMIT = "org.onap.ccsdk.sli.jdbc.limit.max"; /** - * Property key with a properties configuration File for database initial limit + * Property key with a properties configuration File for database initial + * limit */ - public static final String INIT_LIMIT = "org.onap.ccsdk.sli.jdbc.limit.init"; + public static final String INIT_LIMIT = "org.onap.ccsdk.sli.jdbc.limit.init"; /** * Property key with a properties configuration File for database hosts @@ -89,14 +99,10 @@ public abstract class BaseDBConfiguration { public static final String DATABASE_HOSTS = "org.onap.ccsdk.sli.jdbc.hosts"; /** - * default value when the connection timeout is not present or cannot be parsed - */ - private static final int DEFAULT_CONNECTION_TIMEOUT = -1; - - /** - * default value when the request timeout is not present or cannot be parsed + * default value when the connection timeout is not present or cannot be + * parsed. */ - private static final int DEFAULT_REQUEST_TIMEOUT = -1; + private static final String DEFAULT_REJECT_CHANGE_VALUE = "-1"; /** * A set of properties with database configuration information. @@ -106,7 +112,9 @@ public abstract class BaseDBConfiguration { /** * Builds a configuration based on given properties * - * @param properties properties represented by the public constant keys defined by this class + * @param properties + * properties represented by the public constant keys defined by + * this class */ public BaseDBConfiguration(final Properties properties) { this.properties = properties; @@ -115,35 +123,30 @@ public abstract class BaseDBConfiguration { /** * Extracts the connection timeout. * - * @return the connection timeout, or DEFAULT_CONNECTION_TIMEOUT if not present + * @return the connection timeout, or + * DEFAULT_REJECT_CHANGE_VALUE if not present */ public int getConnTimeout() { - return extractProperty(properties, CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); + try { + String value = properties.getProperty(CONNECTION_TIMEOUT, DEFAULT_REJECT_CHANGE_VALUE); + return Integer.parseInt(value); + } catch (Exception exc) { + return Integer.parseInt(DEFAULT_REJECT_CHANGE_VALUE); + } } /** * Extracts the request timeout. * - * @return the request timeout, or DEFAULT_REQUEST_TIMEOUT if not present + * @return the request timeout, or DEFAULT_REQUEST_TIMEOUT if + * not present */ public int getRequestTimeout() { - return extractProperty(properties, REQUEST_TIMEOUT, DEFAULT_REQUEST_TIMEOUT); - } - - /** - * A utility method to extract int property from Properties. - * - * @param properties a set of Properties - * @param propertyKey the key in question - * @param defaultValue the value to return if the key does not exist - * @return Either the property value for propertyKey or defaultValue if not present - */ - private static int extractProperty(final Properties properties, final String propertyKey, final int defaultValue) { try { - final String valueString = properties.getProperty(propertyKey, Integer.toString(defaultValue)); - return Integer.parseInt(valueString); - } catch(final NumberFormatException e) { - return defaultValue; + String value = properties.getProperty(REQUEST_TIMEOUT, DEFAULT_REJECT_CHANGE_VALUE); + return Integer.parseInt(value); + } catch (Exception exc) { + return Integer.parseInt(DEFAULT_REJECT_CHANGE_VALUE); } } @@ -165,6 +168,15 @@ public abstract class BaseDBConfiguration { return properties.getProperty(DATABASE_NAME); } + /** + * Extracts the jdbc driver's name. + * + * @return the jdbc name, or com.mysql.jdbc.Driver if not present + */ + public String getDriverName() { + return properties.getProperty(DRIVER_NAME, "com.mysql.jdbc.Driver"); + } + /** * Extracts the db user id. * @@ -187,7 +199,9 @@ public abstract class BaseDBConfiguration { * Extracts the db min limit. * * @return the db min limit - * @throws NumberFormatException if the property is not specified, or cannot be parsed as an Integer. + * @throws NumberFormatException + * if the property is not specified, or cannot be parsed as an + * Integer. */ public int getDbMinLimit() throws NumberFormatException { String value = properties.getProperty(MIN_LIMIT); @@ -198,7 +212,9 @@ public abstract class BaseDBConfiguration { * Extracts the db max limit. * * @return the db max limit - * @throws NumberFormatException if the property is not specified, or cannot be parsed as an Integer. + * @throws NumberFormatException + * if the property is not specified, or cannot be parsed as an + * Integer. */ public int getDbMaxLimit() throws NumberFormatException { String value = properties.getProperty(MAX_LIMIT); @@ -209,7 +225,9 @@ public abstract class BaseDBConfiguration { * Extracts the db initial limit. * * @return the db initial limit - * @throws NumberFormatException if the property is not specified, or cannot be parsed as an Integer. + * @throws NumberFormatException + * if the property is not specified, or cannot be parsed as an + * Integer. */ public int getDbInitialLimit() throws NumberFormatException { String value = properties.getProperty(INIT_LIMIT); @@ -224,14 +242,4 @@ public abstract class BaseDBConfiguration { public String getDbUrl() { return properties.getProperty(DATABASE_URL); } - - /** - * Extracts the db server group. - * - * @return null - */ - @Deprecated - public String getServerGroup() { - return null; - } } diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/AbstractDBResourceManagerFactory.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/AbstractDBResourceManagerFactory.java deleted file mode 100644 index e88734d2..00000000 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/AbstractDBResourceManagerFactory.java +++ /dev/null @@ -1,39 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * onap - * ================================================================================ - * Copyright (C) 2016 - 2017 ONAP - * ================================================================================ - * 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.factory; - -import org.onap.ccsdk.sli.core.dblib.jdbc.JdbcDbResourceManagerFactory; - -/** - * @version $Revision: 1.1 $ - * Change Log - * Author Date Comments - * ============== ======== ==================================================== - * Rich Tabedzki - */ -public class AbstractDBResourceManagerFactory { - - public static AbstractResourceManagerFactory getFactory(String type) throws FactoryNotDefinedException { - - // JDBC - return JdbcDbResourceManagerFactory.createIntstance(); - } -} diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/AbstractResourceManagerFactory.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/AbstractResourceManagerFactory.java deleted file mode 100644 index 486d0ccc..00000000 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/AbstractResourceManagerFactory.java +++ /dev/null @@ -1,104 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * onap - * ================================================================================ - * Copyright (C) 2016 - 2017 ONAP - * ================================================================================ - * 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.factory; - - -import org.onap.ccsdk.sli.core.dblib.CachedDataSource; -import org.onap.ccsdk.sli.core.dblib.CachedDataSourceFactory; -import org.onap.ccsdk.sli.core.dblib.DBResourceManager; -import org.onap.ccsdk.sli.core.dblib.config.BaseDBConfiguration; -import org.onap.ccsdk.sli.core.dblib.config.DbConfigPool; -import org.onap.ccsdk.sli.core.dblib.config.JDBCConfiguration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.sql.SQLException; -import java.util.Set; -import java.util.concurrent.Callable; - -/** - * @version $Revision: 1.6 $ - * Change Log - * Author Date Comments - * ============== ======== ==================================================== - * Rich Tabedzki - */ -public abstract class AbstractResourceManagerFactory { - private static Logger LOGGER = LoggerFactory.getLogger(AbstractResourceManagerFactory.class); - - public abstract CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager manager) throws Exception; - public abstract CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager dbResourceManager, String sourceName) throws SQLException ; - - - public static AbstractResourceManagerFactory createIntstance() throws FactoryNotDefinedException { - throw new FactoryNotDefinedException("Factory method 'createIntstance' needs to be overriden in DBResourceManagerFactory"); - } - - public class DBInitTask implements Callable - { - private BaseDBConfiguration config = null; - private Set activeTasks; - - public DBInitTask(JDBCConfiguration jdbcconfig, Set tasks) { - this.config = jdbcconfig; - this.activeTasks = tasks; - } - - public CachedDataSource call() throws Exception { - CachedDataSource ds = null; - try { - ds = CachedDataSourceFactory.createDataSource(config); - return ds; - } finally { - synchronized(activeTasks) { - activeTasks.remove(this); - if (activeTasks.isEmpty()) { - final Runnable closure = new Runnable() { - - public void run() { - try { - Thread.sleep(300); - } catch (Exception e) { - } - synchronized(activeTasks) { - activeTasks.notifyAll(); - } - } - }; - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Completed CachedDataSource.Call and notifyAll from " + (ds != null ? ds - .getDbConnectionName() : null)); - } - Thread worker = new Thread(closure); - worker.setDaemon(true); - worker.start(); - } else { - if (LOGGER.isDebugEnabled()) { - if (ds != null) { - LOGGER.debug("Completed CachedDataSource.Call from " + ds.getDbConnectionName()); - } - } - } - } - } - } - } -} diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/FactoryNotDefinedException.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/FactoryNotDefinedException.java deleted file mode 100644 index b6383a53..00000000 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/factory/FactoryNotDefinedException.java +++ /dev/null @@ -1,37 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * onap - * ================================================================================ - * Copyright (C) 2016 - 2017 ONAP - * ================================================================================ - * 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.factory; - - -/** - * @version 1.3 - * Change Log - * Author Date Comments - * ============== ======== ==================================================== - * Rich Tabedzki 01/16/08 Initial version - */ -public class FactoryNotDefinedException extends Exception { - - public FactoryNotDefinedException(String message) { - super(message); - } - -} diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDBCachedDataSource.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDBCachedDataSource.java old mode 100644 new mode 100755 index ffe83445..3ec4f2ed --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDBCachedDataSource.java +++ b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDBCachedDataSource.java @@ -33,19 +33,10 @@ import org.onap.ccsdk.sli.core.dblib.config.BaseDBConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.mysql.jdbc.Driver; - - -/** - * @version $Revision: 1.7 $ - * Change Log - * Author Date Comments - * ============== ======== ==================================================== - * Rich Tabedzki - */ public class JdbcDBCachedDataSource extends CachedDataSource { + private String dbDriver; private String dbUserId; private String dbPasswd; private String dbUrl; @@ -96,15 +87,14 @@ public class JdbcDBCachedDataSource extends CachedDataSource LOGGER.error(AS_CONF_ERROR + errorMsg); throw new DBConfigException(errorMsg); } - /* - dbDriver = jdbcConfig.getDbDriver(); + + dbDriver = jdbcConfig.getDriverName(); if (dbDriver == null) { String errorMsg = "Invalid XML contents: JDBCConnection missing dbDriver attribute"; LOGGER.error(AS_CONF_ERROR + errorMsg); - throw new ScpTblUpdateError(errorMsg); + throw new DBConfigException(errorMsg); } - */ minLimit = jdbcConfig.getDbMinLimit(); // if (minLimit == null) @@ -136,11 +126,10 @@ public class JdbcDBCachedDataSource extends CachedDataSource } try { - Driver dr = new com.mysql.jdbc.Driver(); - Class clazz = Class.forName("com.mysql.jdbc.Driver") ; + Class clazz = Class.forName(dbDriver) ; PoolProperties p = new PoolProperties(); - p.setDriverClassName("com.mysql.jdbc.Driver"); + p.setDriverClassName(dbDriver); p.setUrl(dbUrl); p.setUsername(dbUserId); p.setPassword(dbPasswd); @@ -172,30 +161,9 @@ public class JdbcDBCachedDataSource extends CachedDataSource PreparedStatement st = null; ResultSet rs = null; - try { - con = dataSource.getConnection(); - st = con.prepareStatement("Select 1 FROM DUAL"); - rs = st.executeQuery(); - } catch(Exception exc) { - LOGGER.error(exc.getMessage()); - } finally { - if(rs != null) rs.close(); - if(st != null) st.close(); - if(con != null) con.close(); - } - initialized = true; - LOGGER.info("MySQLDataSource <"+dbConnectionName+"> configured successfully. Using URL: "+dbUrl); + LOGGER.info("JdbcDBCachedDataSource <"+dbConnectionName+"> configured successfully. Using URL: "+dbUrl); } - -// } catch (SQLException exc) { -// initialized = false; -// StringBuffer sb = new StringBuffer(); -// sb.append("Failed to initialize MySQLDataSource<"); -// sb.append(dbConnectionName).append(">. Reason: "); -// sb.append(exc.getMessage()); -// LOGGER.error("AS_CONF_ERROR: " + sb.toString()); -//// throw new DBConfigException(e.getMessage()); } catch (Exception exc) { initialized = false; StringBuffer sb = new StringBuffer(); diff --git a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDbResourceManagerFactory.java b/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDbResourceManagerFactory.java deleted file mode 100644 index 803e6b3e..00000000 --- a/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/jdbc/JdbcDbResourceManagerFactory.java +++ /dev/null @@ -1,183 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * onap - * ================================================================================ - * Copyright (C) 2016 - 2017 ONAP - * ================================================================================ - * 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.jdbc; - - -import java.sql.SQLException; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.FutureTask; - -import org.onap.ccsdk.sli.core.dblib.CachedDataSource; -import org.onap.ccsdk.sli.core.dblib.CachedDataSourceFactory; -import org.onap.ccsdk.sli.core.dblib.DBResourceManager; -import org.onap.ccsdk.sli.core.dblib.config.DbConfigPool; -import org.onap.ccsdk.sli.core.dblib.config.JDBCConfiguration; -import org.onap.ccsdk.sli.core.dblib.factory.AbstractResourceManagerFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * @version $Revision: 1.6 $ - * Change Log - * Author Date Comments - * ============== ======== ==================================================== - * Rich Tabedzki - */ -public class JdbcDbResourceManagerFactory extends AbstractResourceManagerFactory { - private static Logger LOGGER = LoggerFactory.getLogger(JdbcDbResourceManagerFactory.class ); - private JdbcDbResourceManagerFactory(){ - - } - - class MyFutureTask extends FutureTask - { - - public MyFutureTask(Callable result) { - super(result); - } - - } - - public CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager manager, String sourceName) throws SQLException - { - // here create the data sources objects - JDBCConfiguration[] list = dbConfig.getJDBCbSourceArray(); - CachedDataSource[] cachedDS = new CachedDataSource[1]; - - for(int i=0, max=list.length; i tasks = new HashSet(); - if(LOGGER.isDebugEnabled()) { - LOGGER.debug("Creating " + list.length + " datasources."); - } - - for(int i=0, max=list.length; i