[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / util / db / CachedDataSourceWrap.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdnc.util.db;
23
24 import java.io.PrintWriter;
25 import java.sql.Connection;
26 import java.sql.SQLException;
27 import java.sql.SQLFeatureNotSupportedException;
28
29 import javax.sql.DataSource;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class CachedDataSourceWrap implements DataSource {
35
36         private static final Logger log = LoggerFactory.getLogger(CachedDataSourceWrap.class);
37
38         private ThreadLocal<ConnectionWrap> con = new ThreadLocal<>();
39
40         private DataSource dataSource;
41
42         @Override
43         public PrintWriter getLogWriter() throws SQLException {
44                 return dataSource.getLogWriter();
45         }
46
47         @Override
48         public void setLogWriter(PrintWriter out) throws SQLException {
49                 dataSource.setLogWriter(out);
50         }
51
52         @Override
53         public void setLoginTimeout(int seconds) throws SQLException {
54                 dataSource.setLoginTimeout(seconds);
55         }
56
57         @Override
58         public int getLoginTimeout() throws SQLException {
59                 return dataSource.getLoginTimeout();
60         }
61
62         @Override
63         public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
64                 return dataSource.getParentLogger();
65         }
66
67         @Override
68         public <T> T unwrap(Class<T> iface) throws SQLException {
69                 return dataSource.unwrap(iface);
70         }
71
72         @Override
73         public boolean isWrapperFor(Class<?> iface) throws SQLException {
74                 return dataSource.isWrapperFor(iface);
75         }
76
77         @Override
78         public Connection getConnection() throws SQLException {
79                 if (con.get() == null) {
80                         Connection c = dataSource.getConnection();
81                         ConnectionWrap cc = new ConnectionWrap(c);
82                         con.set(cc);
83
84                         log.info("Got new DB connection: " + c);
85                 } else
86                         log.info("Using thread DB connection: " + con.get().getCon());
87
88                 return con.get();
89         }
90
91         @Override
92         public Connection getConnection(String username, String password) throws SQLException {
93                 if (con.get() == null) {
94                         Connection c = dataSource.getConnection(username, password);
95                         ConnectionWrap cc = new ConnectionWrap(c);
96                         con.set(cc);
97
98                         log.info("Got new DB connection: " + c);
99                 } else
100                         log.info("Using thread DB connection: " + con.get().getCon());
101
102                 return con.get();
103         }
104
105         public void releaseConnection() {
106                 if (con.get() != null) {
107                         try {
108                                 con.get().realClose();
109
110                                 log.info("DB Connection released: " + con.get().getCon());
111                         } catch (SQLException e) {
112                                 log.warn("Failed to release DB connection", e);
113                         } finally {
114                                 con.remove();
115                         }
116                 }
117         }
118
119         public void setDataSource(DataSource dataSource) {
120                 this.dataSource = dataSource;
121         }
122 }