diff options
author | Rich Tabedzki <richard.tabedzki@att.com> | 2017-09-15 01:02:37 +0000 |
---|---|---|
committer | Rich Tabedzki <richard.tabedzki@att.com> | 2017-09-15 02:46:44 +0000 |
commit | d3913648518ce15fe7a74275394b86d0ecfb32a4 (patch) | |
tree | 22d9a0acf5a9e4913a538cd31a3510052b3e5d59 /dblib/provider/src | |
parent | 871e2b160680d521a6fc96abe745b96d91c9cee6 (diff) |
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 <richard.tabedzki@att.com>
Diffstat (limited to 'dblib/provider/src')
8 files changed, 210 insertions, 499 deletions
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 index ee8ab2f3..a5902d54 100644..100755 --- 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 index 7a27a20c..a2eb0f9c 100644..100755 --- 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<CachedDataSource> dsQueue = new ConcurrentLinkedQueue<CachedDataSource>(); - Queue<CachedDataSource> dsQueue = new PriorityQueue<>(4, new Comparator<CachedDataSource>() { + Queue<CachedDataSource> dsQueue = new PriorityQueue<CachedDataSource>(4, new Comparator<CachedDataSource>() { @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<CachedDataSource> semaphore = new ConcurrentLinkedQueue<CachedDataSource>(); 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<tester.length; i++){ + tester[i] = new DataSourceTester(cachedDS[i], DBResourceManager.this, semaphore); + tester[i].start(); + } + + // the timeout param is set is seconds. + long timeout = ((dbConfig.getTimeout() <= 0) ? 60L : dbConfig.getTimeout()); + LOGGER.debug("Timeout set to " +timeout+" seconds"); + timeout *= 1000; + + + synchronized (semaphore) { + semaphore.wait(timeout); + } + } catch(Exception exc){ + LOGGER.warn("DBResourceManager.initWorker", exc); + } finally { + startTime = System.currentTimeMillis() - startTime; + LOGGER.info("Completed wait with "+ dsQueue.size() + " active datasource(s) in " + startTime + " ms"); + } + } + + + class DataSourceTester extends Thread { + + private final CachedDataSource ds; + private final DBResourceManager manager; + private final ConcurrentLinkedQueue<CachedDataSource> semaphoreQ; + + public DataSourceTester(CachedDataSource ds, DBResourceManager manager, ConcurrentLinkedQueue<CachedDataSource> 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<CachedDataSource> sources = new LinkedList<>(this.dsQueue); + LinkedList<CachedDataSource> sources = new LinkedList<CachedDataSource>(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<dss.length; i++) { if(!dss[i].isSlave()) { @@ -496,7 +590,12 @@ public class DBResourceManager implements DataSource, DataAccessor, DBResourceOb return true; } - private void setDataSource(CachedDataSource dataSource) { + public void setDataSource(CachedDataSource dataSource) { + if(this.dsQueue.contains(dataSource)) + return; + if(this.broken.contains(dataSource)) + return; + if(dataSource.testConnection(true)){ this.dsQueue.add(dataSource); } else { @@ -525,16 +624,12 @@ public class DBResourceManager implements DataSource, DataAccessor, DBResourceOb lastException = exc; } catch(PoolExhaustedException exc) { throw new NoAvailableConnectionsException(exc); - } catch(MySQLNonTransientConnectionException exc){ - throw new NoAvailableConnectionsException(exc); } catch(Exception exc){ lastException = exc; if(recoveryMode){ handleGetConnectionException(active, exc); } else { - if(exc instanceof MySQLNonTransientConnectionException) { - throw new NoAvailableConnectionsException(exc); - } if(exc instanceof SQLException) { + if(exc instanceof SQLException) { throw (SQLException)exc; } else { DBLibException excptn = new DBLibException(exc.getMessage()); @@ -700,7 +795,7 @@ public class DBResourceManager implements DataSource, DataAccessor, DBResourceOb public String getDBStatus(boolean htmlFormat) { StringBuilder buffer = new StringBuilder(); - ArrayList<CachedDataSource> list = new ArrayList<>(); + ArrayList<CachedDataSource> list = new ArrayList<CachedDataSource>(); 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<CachedDataSource> snapshot = new LinkedList<>(dsQueue); + LinkedList<CachedDataSource> snapshot = new LinkedList<CachedDataSource>(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 index d53be0e7..a8bc58b8 100644..100755 --- 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 - * <code>org.onap.ccsdk.sli.jdbc.user</code>. + * Property key with a properties configuration File for database password + * for associated with <code>org.onap.ccsdk.sli.jdbc.user</code>. */ - 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 <code>DEFAULT_CONNECTION_TIMEOUT</code> if not present + * @return the connection timeout, or + * <code>DEFAULT_REJECT_CHANGE_VALUE</code> 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 <code>DEFAULT_REQUEST_TIMEOUT</code> if not present + * @return the request timeout, or <code>DEFAULT_REQUEST_TIMEOUT</code> 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 <code>Properties</code> - * @param propertyKey the key in question - * @param defaultValue the value to return if the key does not exist - * @return Either the property value for <code>propertyKey</code> or <code>defaultValue</code> 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); } } @@ -166,6 +169,15 @@ public abstract class BaseDBConfiguration { } /** + * Extracts the jdbc driver's name. + * + * @return the jdbc name, or <code>com.mysql.jdbc.Driver</code> if not present + */ + public String getDriverName() { + return properties.getProperty(DRIVER_NAME, "com.mysql.jdbc.Driver"); + } + + /** * Extracts the db user id. * * @return the db user id, or <code>null</code> if not present @@ -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 <code>Integer</code>. + * @throws NumberFormatException + * if the property is not specified, or cannot be parsed as an + * <code>Integer</code>. */ 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 <code>Integer</code>. + * @throws NumberFormatException + * if the property is not specified, or cannot be parsed as an + * <code>Integer</code>. */ 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 <code>Integer</code>. + * @throws NumberFormatException + * if the property is not specified, or cannot be parsed as an + * <code>Integer</code>. */ 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 <code>null</code> - */ - @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<CachedDataSource> - { - private BaseDBConfiguration config = null; - private Set<DBInitTask> activeTasks; - - public DBInitTask(JDBCConfiguration jdbcconfig, Set<DBInitTask> 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 index ffe83445..3ec4f2ed 100644..100755 --- 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<CachedDataSource> - { - - public MyFutureTask(Callable<CachedDataSource> 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<max; i++){ - if(!sourceName.equals(list[i].getDbConnectionName())) - continue; - - JDBCConfiguration config = list[i]; - CachedDataSource dataSource = CachedDataSourceFactory.createDataSource(config); - cachedDS[0] = dataSource; - } - return cachedDS; - } - - public CachedDataSource[] initDBResourceManager(DbConfigPool dbConfig, DBResourceManager manager) /* throws Exception */ { - - ExecutorService threadExecutor = Executors.newFixedThreadPool(2); - // here create the data sources objects - JDBCConfiguration[] list = dbConfig.getJDBCbSourceArray(); - - MyFutureTask[] futures = new MyFutureTask[list.length]; - final Set<DBInitTask> tasks = new HashSet<DBInitTask>(); - if(LOGGER.isDebugEnabled()) { - LOGGER.debug("Creating " + list.length + " datasources."); - } - - for(int i=0, max=list.length; i<max; i++){ - JDBCConfiguration config = list[i]; - - DBInitTask task = new DBInitTask(config, tasks); - tasks.add(task); - futures[i] = new MyFutureTask(task); - } - - try { - synchronized(tasks){ - for(int i=0, max=list.length; i<max; i++){ - if(LOGGER.isDebugEnabled()) - LOGGER.debug("Starting executor tasks."); - threadExecutor.execute(futures[i]); - } - // the timeout param is set is seconds. - long timeout = ((dbConfig.getTimeout() <= 0) ? 60L : dbConfig.getTimeout()); - LOGGER.debug("Timeout set to " +timeout+" seconds"); - timeout *= 1000; - // the timeout param is set is seconds, hence it needs to be multiplied by 1000. - tasks.wait(timeout); - if(LOGGER.isDebugEnabled()) - LOGGER.debug("initDBResourceManager wait completed."); - } - } catch(Exception exc) { - LOGGER.error("Failed to initialize JndiCachedDataSource. Reason: ", exc); - } - - if(threadExecutor != null){ - try { - threadExecutor.shutdown(); - } catch(Exception exc){} - } - - CachedDataSource[] cachedDS = new CachedDataSource[futures.length]; - - boolean initialized = false; - for(int i=0; i<futures.length; i++){ - Object obj = null; - if(futures[i].isDone()){ - try { - obj = futures[i].get(); - if(obj instanceof CachedDataSource){ - cachedDS[i] = (CachedDataSource)obj; - initialized |= cachedDS[i].isInitialized(); - if(cachedDS[i].isInitialized()) - LOGGER.info("DataSource "+list[i].getDbConnectionName()+" initialized successfully"); - else - LOGGER.error("DataSource "+list[i].getDbConnectionName()+" initialization failed"); - } else { - if(obj == null) { - LOGGER.warn("DataSource " + i + " initialization failed. Returned object is null"); - } else { - LOGGER.warn("DataSource " + i + " initialization failed. Returned object is " + obj.getClass().getName()); - } - } - } catch (InterruptedException exc) { - LOGGER.error("DataSource "+list[i].getDbConnectionName()+" initialization failed", exc); - } catch (ExecutionException exc) { - LOGGER.error("DataSource "+list[i].getDbConnectionName()+" initialization failed", exc); - } catch (Exception exc) { - LOGGER.error("DataSource "+list[i].getDbConnectionName()+" initialization failed", exc); - } - } else { - try { - obj = futures[i].get(); - if(obj instanceof CachedDataSource){ - LOGGER.warn("DataSource "+((CachedDataSource)obj).getDbConnectionName()+" failed"); - } else { - if(obj == null) { - LOGGER.warn("DataSource " + i + " initialization failed. Returned object is null"); - } else { - LOGGER.warn("DataSource " + i + " initialization failed. Returned object is " + obj.getClass().getName()); - } - } - } catch (Exception exc) { - LOGGER.error("DataSource "+list[i].getDbConnectionName()+" initialization failed", exc); - } - } - } - - if(!initialized){ - new Error("Failed to initialize DB Library."); - } - return cachedDS; - } - - public static AbstractResourceManagerFactory createIntstance() { - return new JdbcDbResourceManagerFactory(); - } -} |