aboutsummaryrefslogtreecommitdiffstats
path: root/dblib/provider
diff options
context:
space:
mode:
authorDan Timoney <dtimoney@att.com>2018-02-01 16:57:00 -0500
committerDan Timoney <dtimoney@att.com>2018-02-09 17:08:35 -0500
commit131cb46f6109a6099a445cb8e1b12ff5b68ae6de (patch)
tree783f96f27c1bc9292fdd836c488c4dd8a86aeb2d /dblib/provider
parente36be89362fe80a7cd3da69a886c6c1f1c99a65c (diff)
Upgrade sli/core to Nitrogen
Use Apache derby for dblib SingleFeatureTest Change-Id: I6b41f7ede1a98b33824fceea9100e75c1ce8dda4 Issue-ID: CCSDK-175 Signed-off-by: Dan Timoney <dtimoney@att.com> Generalization of CCSDK core/utils framework Changes made: * Created generalized version of core/utils/dblib as core/utils/common * Deprecated core/utils/dblib package Change-Id: I0992c43910278fbe254674d1e39d7e4fcad0a592 Issue-ID: CCSDK-168 Signed-off-by: Rich Tabedzki <richard.tabedzki@att.com> Use Apache derby for dblib test Use Apache derby for dblib SingleFeatureTest Change-Id: Ie497557f162e203fa5c5c82c17ddc55ba0c11b38 Issue-ID: CCSDK-175 Signed-off-by: Dan Timoney <dtimoney@att.com>
Diffstat (limited to 'dblib/provider')
-rwxr-xr-xdblib/provider/pom.xml90
-rwxr-xr-xdblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/CachedDataSource.java1008
-rwxr-xr-xdblib/provider/src/main/resources/dblib.properties7
3 files changed, 554 insertions, 551 deletions
diff --git a/dblib/provider/pom.xml b/dblib/provider/pom.xml
index 3bdd7e67..e6fd1bcb 100755
--- a/dblib/provider/pom.xml
+++ b/dblib/provider/pom.xml
@@ -1,19 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
+
<parent>
- <groupId>org.onap.ccsdk.sli.core</groupId>
- <artifactId>dblib</artifactId>
- <version>0.2.0-SNAPSHOT</version>
+ <groupId>org.onap.ccsdk.parent</groupId>
+ <artifactId>binding-parent</artifactId>
+ <version>1.0.1-SNAPSHOT</version>
+ <relativePath />
</parent>
+
+ <groupId>org.onap.ccsdk.sli.core</groupId>
<artifactId>dblib-provider</artifactId>
- <version>0.2.0-SNAPSHOT</version>
+ <version>0.2.1-SNAPSHOT</version>
<packaging>bundle</packaging>
- <name>DBLIB Adaptor - Provider</name>
+
+ <name>ccsdk-sli-core :: dblib :: ${project.artifactId}</name>
<url>http://maven.apache.org</url>
+
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
+
<dependencies>
<dependency>
<groupId>junit</groupId>
@@ -21,12 +29,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>ch.vorburger.mariaDB4j</groupId>
- <artifactId>mariaDB4j</artifactId>
- <version>2.2.3</version>
- <scope>test</scope>
- </dependency>
+ <dependency>
+ <groupId>ch.vorburger.mariaDB4j</groupId>
+ <artifactId>mariaDB4j</artifactId>
+ <version>2.2.3</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>equinoxSDK381</groupId>
<artifactId>org.eclipse.osgi</artifactId>
@@ -48,53 +56,31 @@
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>org.mariadb.jdbc</groupId>
- <artifactId>mariadb-java-client</artifactId>
- <version>${mariadb.connector.version}</version>
- </dependency>
<dependency>
- <groupId>org.apache.tomcat</groupId>
- <artifactId>tomcat-jdbc</artifactId>
- <version>${tomcat-jdbc.version}</version>
+ <groupId>org.mariadb.jdbc</groupId>
+ <artifactId>mariadb-java-client</artifactId>
+ <version>${mariadb.connector.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tomcat</groupId>
+ <artifactId>tomcat-jdbc</artifactId>
+ <version>${tomcat-jdbc.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
</dependency>
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- </dependency>
<dependency>
<groupId>org.onap.ccsdk.sli.core</groupId>
<artifactId>utils-provider</artifactId>
<version>${project.version}</version>
</dependency>
- <!-- Testing related dependencies -->
- <dependency>
- <groupId>org.mockito</groupId>
- <artifactId>mockito-core</artifactId>
- <scope>test</scope>
- </dependency>
+ <!-- Testing related dependencies -->
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.felix</groupId>
- <artifactId>maven-bundle-plugin</artifactId>
- <version>${bundle.plugin.version}</version>
- <extensions>true</extensions>
- <configuration>
- <instructions>
- <Bundle-SymbolicName>org.onap.ccsdk.sli.core.dblib</Bundle-SymbolicName>
- <Export-Package>org.onap.ccsdk.sli.core.dblib;version=${project.version}</Export-Package>
- <Import-Package>*,org.mariadb.jdbc</Import-Package>
- <Embed-Transitive>true</Embed-Transitive>
- </instructions>
- </configuration>
-
- </plugin>
-
-
- </plugins>
- </build>
</project>
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 d2331786..8dac3361 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
@@ -56,500 +56,516 @@ import org.slf4j.LoggerFactory;
public abstract class CachedDataSource implements DataSource, SQLExecutionMonitorObserver {
- private static final Logger LOGGER = LoggerFactory.getLogger(CachedDataSource.class);
-
- private static final String SQL_FAILURE = "SQL FAILURE. time(ms): ";
- private static final String FAILED_TO_EXECUTE = "> Failed to execute: ";
- private static final String WITH_ARGUMENTS = " with arguments: ";
- private static final String WITH_NO_ARGUMENTS = " with no arguments. ";
- private static final String SQL_DATA_SOURCE = "SQL DataSource <";
-
-
- protected long connReqTimeout = 30L;
- protected long dataReqTimeout = 100L;
-
- private final SQLExecutionMonitor monitor;
- protected DataSource ds = null;
- protected String connectionName = null;
- protected boolean initialized = false;
-
- private long interval = 1000;
- private long initialDelay = 5000;
- private long expectedCompletionTime = 50L;
- private boolean canTakeOffLine = true;
- private long unprocessedFailoverThreshold = 3L;
-
- private long nextErrorReportTime = 0L;
-
- private String globalHostName = null;
-
-
- public CachedDataSource(BaseDBConfiguration jdbcElem) throws DBConfigException {
- configure(jdbcElem);
- monitor = new SQLExecutionMonitor(this);
- }
-
- protected abstract void configure(BaseDBConfiguration jdbcElem) throws DBConfigException;
-
- /* (non-Javadoc)
- * @see javax.sql.DataSource#getConnection()
- */
- @Override
- public Connection getConnection() throws SQLException {
- return ds.getConnection();
- }
-
- public CachedRowSet getData(String statement, List<Object> arguments)
- throws SQLException {
- TestObject testObject = monitor.registerRequest();
-
- try (Connection connection = this.getConnection()) {
- if (connection == null) {
- throw new SQLException("Connection invalid");
- }
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Obtained connection <{}>: {}", connectionName, connection);
- }
- return executePreparedStatement(connection, statement, arguments, true);
- } finally {
- monitor.deregisterRequest(testObject);
- }
- }
-
- public boolean writeData(String statement, List<Object> arguments)
- throws SQLException {
- TestObject testObject = monitor.registerRequest();
-
- try (Connection connection = this.getConnection()) {
- if (connection == null) {
- throw new SQLException("Connection invalid");
- }
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Obtained connection <{}>: {}", connectionName, connection);
- }
- return executeUpdatePreparedStatement(connection, statement, arguments, true);
- } finally {
- monitor.deregisterRequest(testObject);
- }
- }
-
- CachedRowSet executePreparedStatement(Connection conn, String statement,
- List<Object> arguments, boolean close) throws SQLException {
- long time = System.currentTimeMillis();
-
- CachedRowSet data = null;
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("SQL Statement: {}", statement);
- if (arguments != null && !arguments.isEmpty()) {
- LOGGER.debug("Argunments: {}", arguments);
- }
- }
-
- ResultSet rs = null;
- try (PreparedStatement ps = conn.prepareStatement(statement)) {
- data = RowSetProvider.newFactory().createCachedRowSet();
- if (arguments != null) {
- for (int i = 0, max = arguments.size(); i < max; i++) {
- ps.setObject(i + 1, arguments.get(i));
- }
- }
- rs = ps.executeQuery();
- data.populate(rs);
- // Point the rowset Cursor to the start
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("SQL SUCCESS. rows returned: {}, time(ms): {}", data.size(), (System.currentTimeMillis()
- - time));
- }
- } catch (SQLException exc) {
- handleSqlExceptionForExecuteStatement(conn, statement, arguments, exc, time);
- } finally {
- handleFinallyBlockForExecutePreparedStatement(rs, conn, close);
- }
-
- return data;
- }
-
- private void handleSqlExceptionForExecuteStatement(Connection conn, String statement,
- List<Object> arguments, SQLException exc, long time) throws SQLException {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug(SQL_FAILURE + (System.currentTimeMillis() - time));
- }
- try {
- conn.rollback();
- } catch (Exception thr) {
- LOGGER.error(thr.getLocalizedMessage(), thr);
- }
- if (arguments != null && !arguments.isEmpty()) {
- LOGGER.error(String.format("<%s%s%s%s%s", connectionName, FAILED_TO_EXECUTE, statement, WITH_ARGUMENTS,
- arguments), exc);
- } else {
- LOGGER.error(String.format("<%s%s%s%s", connectionName, FAILED_TO_EXECUTE, statement, WITH_NO_ARGUMENTS),
- exc);
- }
- throw exc;
- }
-
- private void handleFinallyBlockForExecutePreparedStatement(ResultSet rs, Connection conn, boolean close) {
- try {
- if (rs != null) {
- rs.close();
- }
- } catch (Exception exc) {
- LOGGER.error(exc.getLocalizedMessage(), exc);
- }
- try {
- if (conn != null && close) {
- conn.close();
- }
- } catch (Exception exc) {
- LOGGER.error(exc.getLocalizedMessage(), exc);
- }
- }
-
- boolean executeUpdatePreparedStatement(Connection conn, String statement, List<Object> arguments,
- boolean close) throws SQLException {
- long time = System.currentTimeMillis();
-
- CachedRowSet data;
-
- try (PreparedStatement ps = conn.prepareStatement(statement)) {
- data = RowSetProvider.newFactory().createCachedRowSet();
- if (arguments != null) {
- prepareStatementForExecuteUpdate(arguments, ps);
- }
- ps.executeUpdate();
- // Point the rowset Cursor to the start
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("SQL SUCCESS. rows returned: {}, time(ms): {}", data.size(), (System.currentTimeMillis()
- - time));
- }
- ps.close();
- } catch (SQLException exc) {
- handleSqlExceptionForExecuteStatement(conn, statement, arguments, exc, time);
- } finally {
- try {
- if (close) {
- conn.close();
- }
- } catch (Exception exc) {
- LOGGER.error(exc.getLocalizedMessage(), exc);
- }
- }
-
- return true;
- }
-
- private void prepareStatementForExecuteUpdate(List<Object> arguments, PreparedStatement ps)
- throws SQLException {
- for (int i = 0, max = arguments.size(); i < max; i++) {
- Object value = arguments.get(i);
- if (value instanceof Blob) {
- ps.setBlob(i + 1, (Blob) value);
- } else if (value instanceof Timestamp) {
- ps.setTimestamp(i + 1, (Timestamp) value);
- } else if (value instanceof Integer) {
- ps.setInt(i + 1, (Integer) value);
- } else if (value instanceof Long) {
- ps.setLong(i + 1, (Long) value);
- } else if (value instanceof Date) {
- ps.setDate(i + 1, (Date) value);
- } else {
- ps.setObject(i + 1, value);
- }
- }
- }
-
- /* (non-Javadoc)
- * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
- */
- @Override
- public Connection getConnection(String username, String password)
- throws SQLException {
- return ds.getConnection(username, password);
- }
-
- /* (non-Javadoc)
- * @see javax.sql.DataSource#getLogWriter()
- */
- @Override
- public PrintWriter getLogWriter() throws SQLException {
- return ds.getLogWriter();
- }
-
- /* (non-Javadoc)
- * @see javax.sql.DataSource#getLoginTimeout()
- */
- @Override
- public int getLoginTimeout() throws SQLException {
- return ds.getLoginTimeout();
- }
-
- /* (non-Javadoc)
- * @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter)
- */
- @Override
- public void setLogWriter(PrintWriter out) throws SQLException {
- ds.setLogWriter(out);
- }
-
- /* (non-Javadoc)
- * @see javax.sql.DataSource#setLoginTimeout(int)
- */
- @Override
- public void setLoginTimeout(int seconds) throws SQLException {
- ds.setLoginTimeout(seconds);
- }
-
-
- @Override
- public final String getDbConnectionName() {
- return connectionName;
- }
-
- protected final void setDbConnectionName(String name) {
- this.connectionName = name;
- }
-
- public void cleanUp() {
- if (ds != null && ds instanceof Closeable) {
- try {
- ((Closeable) ds).close();
- } catch (IOException e) {
- LOGGER.warn(e.getMessage());
- }
- }
- ds = null;
- monitor.deleteObservers();
- monitor.cleanup();
- }
-
- public boolean isInitialized() {
- return initialized;
- }
-
- protected boolean testConnection() {
- return testConnection(false);
- }
-
- protected boolean testConnection(boolean errorLevel) {
- ResultSet rs = null;
- try (Connection conn = this.getConnection(); Statement stmt = conn.createStatement()) {
- Boolean readOnly;
- String hostname;
- rs = stmt.executeQuery(
- "SELECT @@global.read_only, @@global.hostname"); //("SELECT 1 FROM DUAL"); //"select BANNER from SYS.V_$VERSION"
- while (rs.next()) {
- readOnly = rs.getBoolean(1);
- hostname = rs.getString(2);
-
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug(
- SQL_DATA_SOURCE + getDbConnectionName() + "> connected to " + hostname + ", read-only is "
- + readOnly + ", tested successfully ");
- }
- }
- } catch (Exception exc) {
- if (errorLevel) {
- LOGGER.error(
- SQL_DATA_SOURCE + this.getDbConnectionName() + "> test failed. Cause : " + exc.getMessage());
- } else {
- LOGGER.info(
- SQL_DATA_SOURCE + this.getDbConnectionName() + "> test failed. Cause : " + exc.getMessage());
- }
- return false;
- } finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- LOGGER.error(e.getLocalizedMessage(), e);
- }
- }
- }
- return true;
- }
-
- @Override
- public boolean isWrapperFor(Class<?> iface) throws SQLException {
- return false;
- }
-
- @Override
- public <T> T unwrap(Class<T> iface) throws SQLException {
- return null;
- }
-
- @SuppressWarnings("deprecation")
- public void setConnectionCachingEnabled(boolean state) {
-// if(ds != null && ds instanceof OracleDataSource)
-// try {
-// ((OracleDataSource)ds).setConnectionCachingEnabled(true);
-// } catch (SQLException exc) {
-// LOGGER.warn("", exc);
-// }
- }
-
- public void addObserver(Observer observer) {
- monitor.addObserver(observer);
- }
-
- public void deleteObserver(Observer observer) {
- monitor.deleteObserver(observer);
- }
-
- @Override
- public long getInterval() {
- return interval;
- }
-
- @Override
- public long getInitialDelay() {
- return initialDelay;
- }
-
- @Override
- public void setInterval(long value) {
- interval = value;
- }
-
- @Override
- public void setInitialDelay(long value) {
- initialDelay = value;
- }
-
- @Override
- public long getExpectedCompletionTime() {
- return expectedCompletionTime;
- }
-
- @Override
- public void setExpectedCompletionTime(long value) {
- expectedCompletionTime = value;
- }
-
- @Override
- public long getUnprocessedFailoverThreshold() {
- return unprocessedFailoverThreshold;
- }
-
- @Override
- public void setUnprocessedFailoverThreshold(long value) {
- this.unprocessedFailoverThreshold = value;
- }
-
- public boolean canTakeOffLine() {
- return canTakeOffLine;
- }
-
- public void blockImmediateOffLine() {
- canTakeOffLine = false;
- final Thread offLineTimer = new Thread(() -> {
- try {
- Thread.sleep(30000L);
- } catch (Exception exc) {
- LOGGER.error(exc.getLocalizedMessage(), exc);
- } finally {
- canTakeOffLine = true;
- }
- });
- offLineTimer.setDaemon(true);
- offLineTimer.start();
- }
-
- /**
- * @return the monitor
- */
- final SQLExecutionMonitor getMonitor() {
- return monitor;
- }
-
- protected boolean isSlave() throws PoolExhaustedException {
- CachedRowSet rs;
- boolean isSlave;
- String hostname = "UNDETERMINED";
- try {
- boolean localSlave = true;
- rs = this.getData("SELECT @@global.read_only, @@global.hostname", new ArrayList<>());
- while (rs.next()) {
- localSlave = rs.getBoolean(1);
- hostname = rs.getString(2);
- }
- isSlave = localSlave;
- } catch (PoolExhaustedException peexc) {
- throw peexc;
- } catch (Exception e) {
- LOGGER.error("", e);
- isSlave = true;
- }
- if (isSlave) {
- LOGGER.debug("SQL SLAVE : {} on server {}", connectionName, hostname);
- } else {
- LOGGER.debug("SQL MASTER : {} on server {}", connectionName, hostname);
- }
- return isSlave;
- }
-
- public boolean isFabric() {
- return false;
- }
-
- protected boolean lockTable(Connection conn, String tableName) {
- boolean retValue = false;
- String query = "LOCK TABLES " + tableName + " WRITE";
- try (Statement preStmt = conn.createStatement(); Statement lock = conn.prepareStatement(query);
- ResultSet rs = preStmt.executeQuery("GETDATE()")) {
- if (tableName != null) {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Executing 'LOCK TABLES " + tableName + " WRITE' on connection " + conn.toString());
- if ("SVC_LOGIC".equals(tableName)) {
- Exception e = new Exception();
- StringWriter sw = new StringWriter();
- PrintWriter pw = new PrintWriter(sw);
- e.printStackTrace(pw);
- LOGGER.debug(sw.toString());
- }
- }
- lock.execute(query);
- retValue = true;
- }
- } catch (Exception exc) {
- LOGGER.error("", exc);
- retValue = false;
- }
- return retValue;
- }
-
- protected boolean unlockTable(Connection conn) {
- boolean retValue;
- try (Statement lock = conn.createStatement()) {
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Executing 'UNLOCK TABLES' on connection {}", conn);
- }
- retValue = lock.execute("UNLOCK TABLES");
- } catch (Exception exc) {
- LOGGER.error("", exc);
- retValue = false;
- }
- return retValue;
- }
-
- public void getPoolInfo(boolean allocation) {
-
- }
-
- public long getNextErrorReportTime() {
- return nextErrorReportTime;
- }
-
- public void setNextErrorReportTime(long nextTime) {
- this.nextErrorReportTime = nextTime;
- }
-
- public void setGlobalHostName(String hostname) {
- this.globalHostName = hostname;
- }
-
- public String getGlobalHostName() {
- return globalHostName;
- }
+ private static final Logger LOGGER = LoggerFactory.getLogger(CachedDataSource.class);
+
+ private static final String SQL_FAILURE = "SQL FAILURE. time(ms): ";
+ private static final String FAILED_TO_EXECUTE = "> Failed to execute: ";
+ private static final String WITH_ARGUMENTS = " with arguments: ";
+ private static final String WITH_NO_ARGUMENTS = " with no arguments. ";
+ private static final String SQL_DATA_SOURCE = "SQL DataSource <";
+
+ protected long connReqTimeout = 30L;
+ protected long dataReqTimeout = 100L;
+
+ private final SQLExecutionMonitor monitor;
+ protected DataSource ds = null;
+ protected String connectionName = null;
+ protected boolean initialized = false;
+
+ private long interval = 1000;
+ private long initialDelay = 5000;
+ private long expectedCompletionTime = 50L;
+ private boolean canTakeOffLine = true;
+ private long unprocessedFailoverThreshold = 3L;
+
+ private long nextErrorReportTime = 0L;
+
+ private String globalHostName = null;
+
+ private boolean isDerby = false;
+
+ public CachedDataSource(BaseDBConfiguration jdbcElem) throws DBConfigException {
+ configure(jdbcElem);
+
+ if ("org.apache.derby.jdbc.EmbeddedDriver".equals(jdbcElem.getDriverName())) {
+ isDerby = true;
+ }
+ monitor = new SQLExecutionMonitor(this);
+ }
+
+ protected abstract void configure(BaseDBConfiguration jdbcElem) throws DBConfigException;
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.sql.DataSource#getConnection()
+ */
+ @Override
+ public Connection getConnection() throws SQLException {
+ return ds.getConnection();
+ }
+
+ public CachedRowSet getData(String statement, List<Object> arguments) throws SQLException {
+ TestObject testObject = monitor.registerRequest();
+
+ try (Connection connection = this.getConnection()) {
+ if (connection == null) {
+ throw new SQLException("Connection invalid");
+ }
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("Obtained connection <{}>: {}", connectionName, connection);
+ }
+ return executePreparedStatement(connection, statement, arguments, true);
+ } finally {
+ monitor.deregisterRequest(testObject);
+ }
+ }
+
+ public boolean writeData(String statement, List<Object> arguments) throws SQLException {
+ TestObject testObject = monitor.registerRequest();
+
+ try (Connection connection = this.getConnection()) {
+ if (connection == null) {
+ throw new SQLException("Connection invalid");
+ }
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("Obtained connection <{}>: {}", connectionName, connection);
+ }
+ return executeUpdatePreparedStatement(connection, statement, arguments, true);
+ } finally {
+ monitor.deregisterRequest(testObject);
+ }
+ }
+
+ CachedRowSet executePreparedStatement(Connection conn, String statement, List<Object> arguments, boolean close)
+ throws SQLException {
+ long time = System.currentTimeMillis();
+
+ CachedRowSet data = null;
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("SQL Statement: {}", statement);
+ if (arguments != null && !arguments.isEmpty()) {
+ LOGGER.debug("Argunments: {}", arguments);
+ }
+ }
+
+ ResultSet rs = null;
+ try (PreparedStatement ps = conn.prepareStatement(statement)) {
+ data = RowSetProvider.newFactory().createCachedRowSet();
+ if (arguments != null) {
+ for (int i = 0, max = arguments.size(); i < max; i++) {
+ ps.setObject(i + 1, arguments.get(i));
+ }
+ }
+ rs = ps.executeQuery();
+ data.populate(rs);
+ // Point the rowset Cursor to the start
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("SQL SUCCESS. rows returned: {}, time(ms): {}", data.size(),
+ (System.currentTimeMillis() - time));
+ }
+ } catch (SQLException exc) {
+ handleSqlExceptionForExecuteStatement(conn, statement, arguments, exc, time);
+ } finally {
+ handleFinallyBlockForExecutePreparedStatement(rs, conn, close);
+ }
+
+ return data;
+ }
+
+ private void handleSqlExceptionForExecuteStatement(Connection conn, String statement, List<Object> arguments,
+ SQLException exc, long time) throws SQLException {
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug(SQL_FAILURE + (System.currentTimeMillis() - time));
+ }
+ try {
+ conn.rollback();
+ } catch (Exception thr) {
+ LOGGER.error(thr.getLocalizedMessage(), thr);
+ }
+ if (arguments != null && !arguments.isEmpty()) {
+ LOGGER.error(String.format("<%s%s%s%s%s", connectionName, FAILED_TO_EXECUTE, statement, WITH_ARGUMENTS,
+ arguments), exc);
+ } else {
+ LOGGER.error(String.format("<%s%s%s%s", connectionName, FAILED_TO_EXECUTE, statement, WITH_NO_ARGUMENTS),
+ exc);
+ }
+ throw exc;
+ }
+
+ private void handleFinallyBlockForExecutePreparedStatement(ResultSet rs, Connection conn, boolean close) {
+ try {
+ if (rs != null) {
+ rs.close();
+ }
+ } catch (Exception exc) {
+ LOGGER.error(exc.getLocalizedMessage(), exc);
+ }
+ try {
+ if (conn != null && close) {
+ conn.close();
+ }
+ } catch (Exception exc) {
+ LOGGER.error(exc.getLocalizedMessage(), exc);
+ }
+ }
+
+ boolean executeUpdatePreparedStatement(Connection conn, String statement, List<Object> arguments, boolean close)
+ throws SQLException {
+ long time = System.currentTimeMillis();
+
+ CachedRowSet data;
+
+ try (PreparedStatement ps = conn.prepareStatement(statement)) {
+ data = RowSetProvider.newFactory().createCachedRowSet();
+ if (arguments != null) {
+ prepareStatementForExecuteUpdate(arguments, ps);
+ }
+ ps.executeUpdate();
+ // Point the rowset Cursor to the start
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("SQL SUCCESS. rows returned: {}, time(ms): {}", data.size(),
+ (System.currentTimeMillis() - time));
+ }
+ ps.close();
+ } catch (SQLException exc) {
+ handleSqlExceptionForExecuteStatement(conn, statement, arguments, exc, time);
+ } finally {
+ try {
+ if (close) {
+ conn.close();
+ }
+ } catch (Exception exc) {
+ LOGGER.error(exc.getLocalizedMessage(), exc);
+ }
+ }
+
+ return true;
+ }
+
+ private void prepareStatementForExecuteUpdate(List<Object> arguments, PreparedStatement ps) throws SQLException {
+ for (int i = 0, max = arguments.size(); i < max; i++) {
+ Object value = arguments.get(i);
+ if (value instanceof Blob) {
+ ps.setBlob(i + 1, (Blob) value);
+ } else if (value instanceof Timestamp) {
+ ps.setTimestamp(i + 1, (Timestamp) value);
+ } else if (value instanceof Integer) {
+ ps.setInt(i + 1, (Integer) value);
+ } else if (value instanceof Long) {
+ ps.setLong(i + 1, (Long) value);
+ } else if (value instanceof Date) {
+ ps.setDate(i + 1, (Date) value);
+ } else {
+ ps.setObject(i + 1, value);
+ }
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
+ */
+ @Override
+ public Connection getConnection(String username, String password) throws SQLException {
+ return ds.getConnection(username, password);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.sql.DataSource#getLogWriter()
+ */
+ @Override
+ public PrintWriter getLogWriter() throws SQLException {
+ return ds.getLogWriter();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.sql.DataSource#getLoginTimeout()
+ */
+ @Override
+ public int getLoginTimeout() throws SQLException {
+ return ds.getLoginTimeout();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter)
+ */
+ @Override
+ public void setLogWriter(PrintWriter out) throws SQLException {
+ ds.setLogWriter(out);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.sql.DataSource#setLoginTimeout(int)
+ */
+ @Override
+ public void setLoginTimeout(int seconds) throws SQLException {
+ ds.setLoginTimeout(seconds);
+ }
+
+ @Override
+ public final String getDbConnectionName() {
+ return connectionName;
+ }
+
+ protected final void setDbConnectionName(String name) {
+ this.connectionName = name;
+ }
+
+ public void cleanUp() {
+ if (ds != null && ds instanceof Closeable) {
+ try {
+ ((Closeable) ds).close();
+ } catch (IOException e) {
+ LOGGER.warn(e.getMessage());
+ }
+ }
+ ds = null;
+ monitor.deleteObservers();
+ monitor.cleanup();
+ }
+
+ public boolean isInitialized() {
+ return initialized;
+ }
+
+ protected boolean testConnection() {
+ return testConnection(false);
+ }
+
+ protected boolean testConnection(boolean errorLevel) {
+ ResultSet rs = null;
+ try (Connection conn = this.getConnection(); Statement stmt = conn.createStatement()) {
+ Boolean readOnly;
+ String hostname;
+ rs = stmt.executeQuery("SELECT @@global.read_only, @@global.hostname"); // ("SELECT 1 FROM DUAL"); //"select
+ // BANNER from SYS.V_$VERSION"
+ while (rs.next()) {
+ readOnly = rs.getBoolean(1);
+ hostname = rs.getString(2);
+
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug(SQL_DATA_SOURCE + getDbConnectionName() + "> connected to " + hostname
+ + ", read-only is " + readOnly + ", tested successfully ");
+ }
+ }
+ } catch (Exception exc) {
+ if (errorLevel) {
+ LOGGER.error(
+ SQL_DATA_SOURCE + this.getDbConnectionName() + "> test failed. Cause : " + exc.getMessage());
+ } else {
+ LOGGER.info(
+ SQL_DATA_SOURCE + this.getDbConnectionName() + "> test failed. Cause : " + exc.getMessage());
+ }
+ return false;
+ } finally {
+ if (rs != null) {
+ try {
+ rs.close();
+ } catch (SQLException e) {
+ LOGGER.error(e.getLocalizedMessage(), e);
+ }
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public boolean isWrapperFor(Class<?> iface) throws SQLException {
+ return false;
+ }
+
+ @Override
+ public <T> T unwrap(Class<T> iface) throws SQLException {
+ return null;
+ }
+
+ @SuppressWarnings("deprecation")
+ public void setConnectionCachingEnabled(boolean state) {
+ // if(ds != null && ds instanceof OracleDataSource)
+ // try {
+ // ((OracleDataSource)ds).setConnectionCachingEnabled(true);
+ // } catch (SQLException exc) {
+ // LOGGER.warn("", exc);
+ // }
+ }
+
+ public void addObserver(Observer observer) {
+ monitor.addObserver(observer);
+ }
+
+ public void deleteObserver(Observer observer) {
+ monitor.deleteObserver(observer);
+ }
+
+ @Override
+ public long getInterval() {
+ return interval;
+ }
+
+ @Override
+ public long getInitialDelay() {
+ return initialDelay;
+ }
+
+ @Override
+ public void setInterval(long value) {
+ interval = value;
+ }
+
+ @Override
+ public void setInitialDelay(long value) {
+ initialDelay = value;
+ }
+
+ @Override
+ public long getExpectedCompletionTime() {
+ return expectedCompletionTime;
+ }
+
+ @Override
+ public void setExpectedCompletionTime(long value) {
+ expectedCompletionTime = value;
+ }
+
+ @Override
+ public long getUnprocessedFailoverThreshold() {
+ return unprocessedFailoverThreshold;
+ }
+
+ @Override
+ public void setUnprocessedFailoverThreshold(long value) {
+ this.unprocessedFailoverThreshold = value;
+ }
+
+ public boolean canTakeOffLine() {
+ return canTakeOffLine;
+ }
+
+ public void blockImmediateOffLine() {
+ canTakeOffLine = false;
+ final Thread offLineTimer = new Thread(() -> {
+ try {
+ Thread.sleep(30000L);
+ } catch (Exception exc) {
+ LOGGER.error(exc.getLocalizedMessage(), exc);
+ } finally {
+ canTakeOffLine = true;
+ }
+ });
+ offLineTimer.setDaemon(true);
+ offLineTimer.start();
+ }
+
+ /**
+ * @return the monitor
+ */
+ final SQLExecutionMonitor getMonitor() {
+ return monitor;
+ }
+
+ protected boolean isSlave() throws PoolExhaustedException {
+
+ // If using Apache derby, just return false
+ if (isDerby) {
+ return false;
+ }
+ CachedRowSet rs;
+ boolean isSlave;
+ String hostname = "UNDETERMINED";
+ try {
+ boolean localSlave = true;
+ rs = this.getData("SELECT @@global.read_only, @@global.hostname", new ArrayList<>());
+ while (rs.next()) {
+ localSlave = rs.getBoolean(1);
+ hostname = rs.getString(2);
+ }
+ isSlave = localSlave;
+ } catch (PoolExhaustedException peexc) {
+ throw peexc;
+ } catch (Exception e) {
+ LOGGER.error("", e);
+ isSlave = true;
+ }
+ if (isSlave) {
+ LOGGER.debug("SQL SLAVE : {} on server {}", connectionName, hostname);
+ } else {
+ LOGGER.debug("SQL MASTER : {} on server {}", connectionName, hostname);
+ }
+ return isSlave;
+ }
+
+ public boolean isFabric() {
+ return false;
+ }
+
+ protected boolean lockTable(Connection conn, String tableName) {
+ boolean retValue = false;
+ String query = "LOCK TABLES " + tableName + " WRITE";
+ try (Statement preStmt = conn.createStatement();
+ Statement lock = conn.prepareStatement(query);
+ ResultSet rs = preStmt.executeQuery("GETDATE()")) {
+ if (tableName != null) {
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("Executing 'LOCK TABLES " + tableName + " WRITE' on connection " + conn.toString());
+ if ("SVC_LOGIC".equals(tableName)) {
+ Exception e = new Exception();
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ e.printStackTrace(pw);
+ LOGGER.debug(sw.toString());
+ }
+ }
+ lock.execute(query);
+ retValue = true;
+ }
+ } catch (Exception exc) {
+ LOGGER.error("", exc);
+ retValue = false;
+ }
+ return retValue;
+ }
+
+ protected boolean unlockTable(Connection conn) {
+ boolean retValue;
+ try (Statement lock = conn.createStatement()) {
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("Executing 'UNLOCK TABLES' on connection {}", conn);
+ }
+ retValue = lock.execute("UNLOCK TABLES");
+ } catch (Exception exc) {
+ LOGGER.error("", exc);
+ retValue = false;
+ }
+ return retValue;
+ }
+
+ public void getPoolInfo(boolean allocation) {
+
+ }
+
+ public long getNextErrorReportTime() {
+ return nextErrorReportTime;
+ }
+
+ public void setNextErrorReportTime(long nextTime) {
+ this.nextErrorReportTime = nextTime;
+ }
+
+ public void setGlobalHostName(String hostname) {
+ this.globalHostName = hostname;
+ }
+
+ public String getGlobalHostName() {
+ return globalHostName;
+ }
}
diff --git a/dblib/provider/src/main/resources/dblib.properties b/dblib/provider/src/main/resources/dblib.properties
index 3e62308f..e0399cc3 100755
--- a/dblib/provider/src/main/resources/dblib.properties
+++ b/dblib/provider/src/main/resources/dblib.properties
@@ -1,9 +1,10 @@
org.onap.ccsdk.sli.dbtype=jdbc
org.onap.ccsdk.sli.jdbc.hosts=sdnctldb01,sdnctldb02
-org.onap.ccsdk.sli.jdbc.url=jdbc:mysql://DBHOST:3306/sdnctl
+org.onap.ccsdk.sli.jdbc.url=jdbc:derby:memory:sdnctl;create=true
+org.onap.ccsdk.sli.jdbc.driver=org.apache.derby.jdbc.EmbeddedDriver
org.onap.ccsdk.sli.jdbc.database=sdnctl
-org.onap.ccsdk.sli.jdbc.user={user for sdnctl}
-org.onap.ccsdk.sli.jdbc.password={password for sdnctl}
+org.onap.ccsdk.sli.jdbc.user=test
+org.onap.ccsdk.sli.jdbc.password=test
org.onap.ccsdk.sli.jdbc.connection.name=sdnctldb01
org.onap.ccsdk.sli.jdbc.connection.timeout=50