2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2016 - 2017 ONAP
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.sli.core.dblib;
23 import java.sql.Array;
25 import java.sql.CallableStatement;
27 import java.sql.Connection;
28 import java.sql.DatabaseMetaData;
29 import java.sql.NClob;
30 import java.sql.PreparedStatement;
31 import java.sql.SQLClientInfoException;
32 import java.sql.SQLException;
33 import java.sql.SQLWarning;
34 import java.sql.SQLXML;
35 import java.sql.Savepoint;
36 import java.sql.Statement;
37 import java.sql.Struct;
38 import java.util.ArrayList;
40 import java.util.Properties;
41 import java.util.concurrent.Executor;
43 import javax.sql.rowset.CachedRowSet;
45 import org.apache.tomcat.jdbc.pool.PooledConnection;
46 import org.apache.tomcat.jdbc.pool.ProxyConnection;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class DBLibConnection implements Connection {
52 private static Logger LOGGER = LoggerFactory.getLogger(DBLibConnection.class);
54 private final Connection connection;
55 private final CachedDataSource dataSource;
56 private boolean locked = false;
57 private String tableName = null;
59 public DBLibConnection(Connection con, CachedDataSource dataSource){
60 this.connection = con;
61 this.dataSource = dataSource;
63 dataSource.getPoolInfo(true);
66 public boolean lockTable(String tablename) {
67 this.tableName = tablename;
68 return locked = dataSource.lockTable(connection, tableName);
71 public void resetInactivityTimer() {
72 Class<org.apache.tomcat.jdbc.pool.PooledConnection> iface = PooledConnection.class;
74 PooledConnection pc = connection.unwrap(iface);
75 pc.setTimestamp(System.currentTimeMillis());
76 } catch (SQLException e) {
77 LOGGER.warn("Failed resetting timeout timer", e);
81 public boolean unlock() {
82 dataSource.unlockTable(connection);
83 return locked = false;
86 public boolean writeData(String statement, ArrayList<String> arguments) throws SQLException, Throwable
88 ArrayList<Object> newList=new ArrayList<Object>();
89 if(arguments != null && !arguments.isEmpty()) {
90 newList.addAll(arguments);
92 resetInactivityTimer();
93 return dataSource.executeUpdatePreparedStatement(connection, statement, newList, false);
96 public CachedRowSet getData(String statement, ArrayList<String> arguments) throws SQLException, Throwable
98 ArrayList<Object> newList=new ArrayList<Object>();
99 if(arguments != null && !arguments.isEmpty()) {
100 newList.addAll(arguments);
102 resetInactivityTimer();
103 return dataSource.executePreparedStatement(connection, statement, newList, false);
107 public <T> T unwrap(Class<T> iface) throws SQLException {
108 return connection.unwrap(iface);
112 public boolean isWrapperFor(Class<?> iface) throws SQLException {
113 return connection.isWrapperFor(iface);
117 public Statement createStatement() throws SQLException {
118 return connection.createStatement();
122 public PreparedStatement prepareStatement(String sql) throws SQLException {
123 return connection.prepareStatement(sql);
127 public CallableStatement prepareCall(String sql) throws SQLException {
128 return connection.prepareCall(sql);
132 public String nativeSQL(String sql) throws SQLException {
133 return connection.nativeSQL(sql);
137 public void setAutoCommit(boolean autoCommit) throws SQLException {
138 connection.setAutoCommit(autoCommit);
142 public boolean getAutoCommit() throws SQLException {
143 return connection.getAutoCommit();
147 public void commit() throws SQLException {
152 public void rollback() throws SQLException {
153 connection.rollback();
157 public void close() throws SQLException {
161 } catch(Throwable th) {
162 LOGGER.error("Failed unlocking",th);
165 if(connection != null && !connection.isClosed()) {
168 dataSource.getPoolInfo(false);
172 public boolean isClosed() throws SQLException {
173 return connection.isClosed();
177 public DatabaseMetaData getMetaData() throws SQLException {
178 return connection.getMetaData();
182 public void setReadOnly(boolean readOnly) throws SQLException {
183 connection.setReadOnly(readOnly);
187 public boolean isReadOnly() throws SQLException {
188 return connection.isReadOnly();
192 public void setCatalog(String catalog) throws SQLException {
193 connection.setCatalog(catalog);
197 public String getCatalog() throws SQLException {
198 return connection.getCatalog();
202 public void setTransactionIsolation(int level) throws SQLException {
203 connection.setTransactionIsolation(level);
207 public int getTransactionIsolation() throws SQLException {
208 return connection.getTransactionIsolation();
212 public SQLWarning getWarnings() throws SQLException {
213 return connection.getWarnings();
217 public void clearWarnings() throws SQLException {
218 connection.clearWarnings();
222 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
223 return connection.createStatement(resultSetType, resultSetConcurrency);
227 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
228 throws SQLException {
229 return connection.prepareStatement(sql, resultSetType, resultSetConcurrency);
233 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
234 return connection.prepareCall(sql, resultSetType, resultSetConcurrency);
238 public Map<String, Class<?>> getTypeMap() throws SQLException {
239 return connection.getTypeMap();
243 public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
244 connection.setTypeMap(map);
248 public void setHoldability(int holdability) throws SQLException {
249 connection.setHoldability(holdability);
253 public int getHoldability() throws SQLException {
254 return connection.getHoldability();
258 public Savepoint setSavepoint() throws SQLException {
259 return connection.setSavepoint();
263 public Savepoint setSavepoint(String name) throws SQLException {
264 return connection.setSavepoint(name);
268 public void rollback(Savepoint savepoint) throws SQLException {
269 connection.rollback(savepoint);
273 public void releaseSavepoint(Savepoint savepoint) throws SQLException {
274 connection.releaseSavepoint(savepoint);
278 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
279 throws SQLException {
280 return connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
284 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
285 int resultSetHoldability) throws SQLException {
286 return connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
290 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
291 int resultSetHoldability) throws SQLException {
292 return connection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
296 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
297 return connection.prepareStatement(sql, autoGeneratedKeys);
301 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
302 return connection.prepareStatement(sql, columnIndexes);
306 public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
307 return connection.prepareStatement(sql, columnNames);
311 public Clob createClob() throws SQLException {
312 return connection.createClob();
316 public Blob createBlob() throws SQLException {
317 return connection.createBlob();
321 public NClob createNClob() throws SQLException {
322 return connection.createNClob();
326 public SQLXML createSQLXML() throws SQLException {
327 return connection.createSQLXML();
331 public boolean isValid(int timeout) throws SQLException {
332 return connection.isValid(timeout);
336 public void setClientInfo(String name, String value) throws SQLClientInfoException {
337 connection.setClientInfo(name, value);
341 public void setClientInfo(Properties properties) throws SQLClientInfoException {
342 connection.setClientInfo(properties);
346 public String getClientInfo(String name) throws SQLException {
347 return connection.getClientInfo(name);
351 public Properties getClientInfo() throws SQLException {
352 return connection.getClientInfo();
356 public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
357 return connection.createArrayOf(typeName, elements);
361 public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
362 return connection.createStruct(typeName, attributes);
366 public void setSchema(String schema) throws SQLException {
367 connection.setSchema(schema);
371 public String getSchema() throws SQLException {
372 return connection.getSchema();
376 public void abort(Executor executor) throws SQLException {
377 connection.abort(executor);
381 public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
382 connection.setNetworkTimeout(executor, milliseconds);
386 public int getNetworkTimeout() throws SQLException {
387 return connection.getNetworkTimeout();