[CCSDK-245] RA: Refactor RA to make it generic
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / onap / ccsdk / sli / adaptors / util / db / CachedDataSourceWrap.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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.onap.ccsdk.sli.adaptors.util.db;
23
24 import java.io.PrintWriter;
25 import java.sql.Connection;
26 import java.sql.SQLException;
27 import java.sql.SQLFeatureNotSupportedException;
28 import javax.sql.DataSource;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class CachedDataSourceWrap implements DataSource {
33
34     private static final Logger log = LoggerFactory.getLogger(CachedDataSourceWrap.class);
35
36     private ThreadLocal<ConnectionWrap> con = new ThreadLocal<>();
37     private ThreadLocal<Boolean> autoCommit = new ThreadLocal<>();
38
39     private DataSource dataSource;
40
41     @Override
42     public PrintWriter getLogWriter() throws SQLException {
43         return dataSource.getLogWriter();
44     }
45
46     @Override
47     public void setLogWriter(PrintWriter out) throws SQLException {
48         dataSource.setLogWriter(out);
49     }
50
51     @Override
52     public void setLoginTimeout(int seconds) throws SQLException {
53         dataSource.setLoginTimeout(seconds);
54     }
55
56     @Override
57     public int getLoginTimeout() throws SQLException {
58         return dataSource.getLoginTimeout();
59     }
60
61     @Override
62     public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
63         return dataSource.getParentLogger();
64     }
65
66     @Override
67     public <T> T unwrap(Class<T> iface) throws SQLException {
68         return dataSource.unwrap(iface);
69     }
70
71     @Override
72     public boolean isWrapperFor(Class<?> iface) throws SQLException {
73         return dataSource.isWrapperFor(iface);
74     }
75
76     @Override
77     public Connection getConnection() throws SQLException {
78         if (con.get() == null) {
79             Connection c = dataSource.getConnection();
80
81             ConnectionWrap cc = new ConnectionWrap(c);
82             con.set(cc);
83
84             autoCommit.set(c.getAutoCommit());
85             c.setAutoCommit(false);
86
87             log.info("Got new DB connection: " + c);
88         } else {
89             log.info("Using thread DB connection: " + con.get().getCon());
90         }
91
92         return con.get();
93     }
94
95     @Override
96     public Connection getConnection(String username, String password) throws SQLException {
97         if (con.get() == null) {
98             Connection c = dataSource.getConnection(username, password);
99
100             ConnectionWrap cc = new ConnectionWrap(c);
101             con.set(cc);
102
103             autoCommit.set(c.getAutoCommit());
104             c.setAutoCommit(false);
105
106             log.info("Got new DB connection: " + c);
107         } else {
108             log.info("Using thread DB connection: " + con.get().getCon());
109         }
110
111         return con.get();
112     }
113
114     public void releaseConnection() {
115         if (con.get() != null) {
116             try {
117                 con.get().setAutoCommit(autoCommit.get());
118                 con.get().realClose();
119
120                 log.info("DB Connection released: " + con.get().getCon());
121             } catch (SQLException e) {
122                 log.warn("Failed to release DB connection", e);
123             } finally {
124                 con.remove();
125             }
126         }
127     }
128
129     public void commit() {
130         if (con.get() != null) {
131             try {
132                 con.get().commit();
133
134                 log.info("DB Connection committed: " + con.get().getCon());
135             } catch (Exception e) {
136                 log.warn("Failed to commit DB connection", e);
137             }
138         }
139     }
140
141     public void rollback() {
142         if (con.get() != null) {
143             try {
144                 con.get().rollback();
145
146                 log.info("DB Connection rolled back: " + con.get().getCon());
147             } catch (Exception e) {
148                 log.warn("Failed to roll back DB connection", e);
149             }
150         }
151     }
152
153     public void setDataSource(DataSource dataSource) {
154         this.dataSource = dataSource;
155     }
156 }