5c1f51040e15fb4f95e3e69f19a838271fe15d6a
[ccsdk/sli/core.git] / dblib / provider / src / main / java / org / openecomp / sdnc / sli / resource / dblib / DBLibConnection.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openecomp
4  * ================================================================================
5  * Copyright (C) 2016 - 2017 AT&T
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdnc.sli.resource.dblib;
22
23 import java.sql.Array;
24 import java.sql.Blob;
25 import java.sql.CallableStatement;
26 import java.sql.Clob;
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;
39 import java.util.Map;
40 import java.util.Properties;
41 import java.util.concurrent.Executor;
42
43 import javax.sql.rowset.CachedRowSet;
44
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;
49
50 public class DBLibConnection implements Connection {
51
52         private static Logger LOGGER = LoggerFactory.getLogger(DBLibConnection.class);
53
54         private final Connection connection;
55         private final CachedDataSource dataSource;
56         private boolean locked = false;
57         private String tableName = null;
58
59         public DBLibConnection(Connection con, CachedDataSource dataSource){
60                 this.connection = con;
61                 this.dataSource = dataSource;
62                 locked = false;
63                 dataSource.getPoolInfo(true);
64         }
65
66         public boolean lockTable(String tablename) {
67                 this.tableName = tablename;
68                 return locked = dataSource.lockTable(connection, tableName);
69         }
70
71         public void resetInactivityTimer() {
72                 Class<org.apache.tomcat.jdbc.pool.PooledConnection> iface = PooledConnection.class;
73                 try {
74                         PooledConnection pc = connection.unwrap(iface);
75                         pc.setTimestamp(System.currentTimeMillis());
76                 } catch (SQLException e) {
77                         LOGGER.warn("Failed resetting timeout timer", e);
78                 }
79         }
80
81         public boolean unlock() {
82                 dataSource.unlockTable(connection);
83                 return locked = false;
84         }
85
86         public boolean writeData(String statement, ArrayList<String> arguments) throws SQLException, Throwable
87         {
88                 ArrayList<Object> newList=new ArrayList<Object>();
89                 if(arguments != null && !arguments.isEmpty()) {
90                         newList.addAll(arguments);
91                 }
92                 resetInactivityTimer();
93                 return dataSource.executeUpdatePreparedStatement(connection, statement, newList, false);
94         }
95
96         public CachedRowSet getData(String statement, ArrayList<String> arguments) throws SQLException, Throwable
97         {
98                 ArrayList<Object> newList=new ArrayList<Object>();
99                 if(arguments != null && !arguments.isEmpty()) {
100                         newList.addAll(arguments);
101                 }
102                 resetInactivityTimer();
103                 return dataSource.executePreparedStatement(connection, statement, newList, false);
104         }
105
106         @Override
107         public <T> T unwrap(Class<T> iface) throws SQLException {
108                 return connection.unwrap(iface);
109         }
110
111         @Override
112         public boolean isWrapperFor(Class<?> iface) throws SQLException {
113                 return connection.isWrapperFor(iface);
114         }
115
116         @Override
117         public Statement createStatement() throws SQLException {
118                 return connection.createStatement();
119         }
120
121         @Override
122         public PreparedStatement prepareStatement(String sql) throws SQLException {
123                 return connection.prepareStatement(sql);
124         }
125
126         @Override
127         public CallableStatement prepareCall(String sql) throws SQLException {
128                 return connection.prepareCall(sql);
129         }
130
131         @Override
132         public String nativeSQL(String sql) throws SQLException {
133                 return connection.nativeSQL(sql);
134         }
135
136         @Override
137         public void setAutoCommit(boolean autoCommit) throws SQLException {
138                 connection.setAutoCommit(autoCommit);
139         }
140
141         @Override
142         public boolean getAutoCommit() throws SQLException {
143                 return connection.getAutoCommit();
144         }
145
146         @Override
147         public void commit() throws SQLException {
148                 connection.commit();
149         }
150
151         @Override
152         public void rollback() throws SQLException {
153                 connection.rollback();
154         }
155
156         @Override
157         public void close() throws SQLException {
158                 if(this.locked) {
159                         try {
160                                 this.unlock();
161                         } catch(Throwable th) {
162                                 LOGGER.error("Failed unlocking",th);
163                         }
164                 }
165                 if(connection != null && !connection.isClosed()) {
166                         connection.close();
167                 }
168                 dataSource.getPoolInfo(false);
169         }
170
171         @Override
172         public boolean isClosed() throws SQLException {
173                 return connection.isClosed();
174         }
175
176         @Override
177         public DatabaseMetaData getMetaData() throws SQLException {
178                 return connection.getMetaData();
179         }
180
181         @Override
182         public void setReadOnly(boolean readOnly) throws SQLException {
183                 connection.setReadOnly(readOnly);
184         }
185
186         @Override
187         public boolean isReadOnly() throws SQLException {
188                 return connection.isReadOnly();
189         }
190
191         @Override
192         public void setCatalog(String catalog) throws SQLException {
193                 connection.setCatalog(catalog);
194         }
195
196         @Override
197         public String getCatalog() throws SQLException {
198                 return connection.getCatalog();
199         }
200
201         @Override
202         public void setTransactionIsolation(int level) throws SQLException {
203                 connection.setTransactionIsolation(level);
204         }
205
206         @Override
207         public int getTransactionIsolation() throws SQLException {
208                 return connection.getTransactionIsolation();
209         }
210
211         @Override
212         public SQLWarning getWarnings() throws SQLException {
213                 return connection.getWarnings();
214         }
215
216         @Override
217         public void clearWarnings() throws SQLException {
218                 connection.clearWarnings();
219         }
220
221         @Override
222         public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
223                 return connection.createStatement(resultSetType, resultSetConcurrency);
224         }
225
226         @Override
227         public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
228                         throws SQLException {
229                 return connection.prepareStatement(sql, resultSetType, resultSetConcurrency);
230         }
231
232         @Override
233         public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
234                 return connection.prepareCall(sql, resultSetType, resultSetConcurrency);
235         }
236
237         @Override
238         public Map<String, Class<?>> getTypeMap() throws SQLException {
239                 return connection.getTypeMap();
240         }
241
242         @Override
243         public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
244                 connection.setTypeMap(map);
245         }
246
247         @Override
248         public void setHoldability(int holdability) throws SQLException {
249                 connection.setHoldability(holdability);
250         }
251
252         @Override
253         public int getHoldability() throws SQLException {
254                 return connection.getHoldability();
255         }
256
257         @Override
258         public Savepoint setSavepoint() throws SQLException {
259                 return connection.setSavepoint();
260         }
261
262         @Override
263         public Savepoint setSavepoint(String name) throws SQLException {
264                 return connection.setSavepoint(name);
265         }
266
267         @Override
268         public void rollback(Savepoint savepoint) throws SQLException {
269                 connection.rollback(savepoint);
270         }
271
272         @Override
273         public void releaseSavepoint(Savepoint savepoint) throws SQLException {
274                 connection.releaseSavepoint(savepoint);
275         }
276
277         @Override
278         public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
279                         throws SQLException {
280                 return connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
281         }
282
283         @Override
284         public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
285                         int resultSetHoldability) throws SQLException {
286                 return connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
287         }
288
289         @Override
290         public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
291                         int resultSetHoldability) throws SQLException {
292                 return connection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
293         }
294
295         @Override
296         public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
297                 return connection.prepareStatement(sql, autoGeneratedKeys);
298         }
299
300         @Override
301         public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
302                 return connection.prepareStatement(sql, columnIndexes);
303         }
304
305         @Override
306         public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
307                 return connection.prepareStatement(sql, columnNames);
308         }
309
310         @Override
311         public Clob createClob() throws SQLException {
312                 return connection.createClob();
313         }
314
315         @Override
316         public Blob createBlob() throws SQLException {
317                 return connection.createBlob();
318         }
319
320         @Override
321         public NClob createNClob() throws SQLException {
322                 return connection.createNClob();
323         }
324
325         @Override
326         public SQLXML createSQLXML() throws SQLException {
327                 return connection.createSQLXML();
328         }
329
330         @Override
331         public boolean isValid(int timeout) throws SQLException {
332                 return connection.isValid(timeout);
333         }
334
335         @Override
336         public void setClientInfo(String name, String value) throws SQLClientInfoException {
337                 connection.setClientInfo(name, value);
338         }
339
340         @Override
341         public void setClientInfo(Properties properties) throws SQLClientInfoException {
342                 connection.setClientInfo(properties);
343         }
344
345         @Override
346         public String getClientInfo(String name) throws SQLException {
347                 return connection.getClientInfo(name);
348         }
349
350         @Override
351         public Properties getClientInfo() throws SQLException {
352                 return connection.getClientInfo();
353         }
354
355         @Override
356         public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
357                 return connection.createArrayOf(typeName, elements);
358         }
359
360         @Override
361         public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
362                 return connection.createStruct(typeName, attributes);
363         }
364
365         @Override
366         public void setSchema(String schema) throws SQLException {
367                 connection.setSchema(schema);
368         }
369
370         @Override
371         public String getSchema() throws SQLException {
372                 return connection.getSchema();
373         }
374
375         @Override
376         public void abort(Executor executor) throws SQLException {
377                 connection.abort(executor);
378         }
379
380         @Override
381         public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
382                 connection.setNetworkTimeout(executor, milliseconds);
383         }
384
385         @Override
386         public int getNetworkTimeout() throws SQLException {
387                 return connection.getNetworkTimeout();
388         }
389
390 }