2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.sli.adaptors.util.db;
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;
32 public class CachedDataSourceWrap implements DataSource {
34 private static final Logger log = LoggerFactory.getLogger(CachedDataSourceWrap.class);
36 private ThreadLocal<ConnectionWrap> con = new ThreadLocal<>();
37 private ThreadLocal<Boolean> autoCommit = new ThreadLocal<>();
39 private DataSource dataSource;
42 public PrintWriter getLogWriter() throws SQLException {
43 return dataSource.getLogWriter();
47 public void setLogWriter(PrintWriter out) throws SQLException {
48 dataSource.setLogWriter(out);
52 public void setLoginTimeout(int seconds) throws SQLException {
53 dataSource.setLoginTimeout(seconds);
57 public int getLoginTimeout() throws SQLException {
58 return dataSource.getLoginTimeout();
62 public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
63 return dataSource.getParentLogger();
67 public <T> T unwrap(Class<T> iface) throws SQLException {
68 return dataSource.unwrap(iface);
72 public boolean isWrapperFor(Class<?> iface) throws SQLException {
73 return dataSource.isWrapperFor(iface);
77 public Connection getConnection() throws SQLException {
78 if (con.get() == null) {
79 Connection c = dataSource.getConnection();
81 ConnectionWrap cc = new ConnectionWrap(c);
84 autoCommit.set(c.getAutoCommit());
85 c.setAutoCommit(false);
87 log.info("Got new DB connection: " + c);
89 log.info("Using thread DB connection: " + con.get().getCon());
96 public Connection getConnection(String username, String password) throws SQLException {
97 if (con.get() == null) {
98 Connection c = dataSource.getConnection(username, password);
100 ConnectionWrap cc = new ConnectionWrap(c);
103 autoCommit.set(c.getAutoCommit());
104 c.setAutoCommit(false);
106 log.info("Got new DB connection: " + c);
108 log.info("Using thread DB connection: " + con.get().getCon());
114 public void releaseConnection() {
115 if (con.get() != null) {
117 con.get().setAutoCommit(autoCommit.get());
118 con.get().realClose();
120 log.info("DB Connection released: " + con.get().getCon());
121 } catch (SQLException e) {
122 log.warn("Failed to release DB connection", e);
129 public void commit() {
130 if (con.get() != null) {
134 log.info("DB Connection committed: " + con.get().getCon());
135 } catch (Exception e) {
136 log.warn("Failed to commit DB connection", e);
141 public void rollback() {
142 if (con.get() != null) {
144 con.get().rollback();
146 log.info("DB Connection rolled back: " + con.get().getCon());
147 } catch (Exception e) {
148 log.warn("Failed to roll back DB connection", e);
153 public void setDataSource(DataSource dataSource) {
154 this.dataSource = dataSource;