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;
 
  29 import javax.sql.DataSource;
 
  31 import org.slf4j.Logger;
 
  32 import org.slf4j.LoggerFactory;
 
  34 public class CachedDataSourceWrap implements DataSource {
 
  36     private static final Logger log = LoggerFactory.getLogger(CachedDataSourceWrap.class);
 
  38     private ThreadLocal<ConnectionWrap> con = new ThreadLocal<>();
 
  40     private DataSource dataSource;
 
  43     public PrintWriter getLogWriter() throws SQLException {
 
  44         return dataSource.getLogWriter();
 
  48     public void setLogWriter(PrintWriter out) throws SQLException {
 
  49         dataSource.setLogWriter(out);
 
  53     public void setLoginTimeout(int seconds) throws SQLException {
 
  54         dataSource.setLoginTimeout(seconds);
 
  58     public int getLoginTimeout() throws SQLException {
 
  59         return dataSource.getLoginTimeout();
 
  63     public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
 
  64         return dataSource.getParentLogger();
 
  68     public <T> T unwrap(Class<T> iface) throws SQLException {
 
  69         return dataSource.unwrap(iface);
 
  73     public boolean isWrapperFor(Class<?> iface) throws SQLException {
 
  74         return dataSource.isWrapperFor(iface);
 
  78     public Connection getConnection() throws SQLException {
 
  79         if (con.get() == null) {
 
  80             Connection c = dataSource.getConnection();
 
  81             ConnectionWrap cc = new ConnectionWrap(c);
 
  84             log.info("Got new DB connection: " + c);
 
  86             log.info("Using thread DB connection: " + con.get().getCon());
 
  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);
 
  98             log.info("Got new DB connection: " + c);
 
 100             log.info("Using thread DB connection: " + con.get().getCon());
 
 105     public void releaseConnection() {
 
 106         if (con.get() != null) {
 
 108                 con.get().realClose();
 
 110                 log.info("DB Connection released: " + con.get().getCon());
 
 111             } catch (SQLException e) {
 
 112                 log.warn("Failed to release DB connection", e);
 
 119     public void setDataSource(DataSource dataSource) {
 
 120         this.dataSource = dataSource;