2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.dao.util.dbcp;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.apache.commons.dbcp2.BasicDataSource;
29 import org.onap.appc.dao.util.api.DBConnectionPoolService;
30 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
32 import java.sql.Connection;
33 import java.sql.SQLException;
34 import java.util.HashMap;
38 * This class provides ability to create database connection pool.
40 public class DBConnectionPool implements DBConnectionPoolService {
42 private final EELFLogger logger = EELFManager.getInstance().getLogger(DBConnectionPool.class);
43 private BasicDataSource dataSource;
45 public enum DataSourceStatus {
46 ACTIVE_NUMBER("active_number"),
47 IDLE_NUMBER("idle_number");
49 private String attribute;
51 DataSourceStatus(String attribute) {
52 this.attribute = attribute;
55 public String getAttribute() {
60 public DBConnectionPool(String connectURI, String username, String password, String driverClass) {
61 this(connectURI, username, password, driverClass, null, null, null, null, null);
64 public DBConnectionPool(String connectURI, String username, String password,
65 String driverClass, Integer initialSize, Integer maxActive,
66 Integer maxIdle, Integer maxWait, Integer minIdle) {
67 this.dataSource = getBasicDataSource(connectURI, username, password, driverClass,
68 initialSize, maxActive, maxIdle, maxWait, minIdle);
72 * Get a connection from datasource which is thread safe.
76 public Connection getConnection() throws DBConnectionPoolException {
77 if (dataSource == null) {
78 throw new DBConnectionPoolException();
81 Connection connection = null;
83 connection = dataSource.getConnection();
84 } catch (SQLException e) {
85 logger.error("Get connection failure", e);
86 throw new DBConnectionPoolException(e);
89 if(connection == null){
91 throw new DBConnectionPoolException("Connection was not created");
98 * Closes and releases all idle connections that are currently stored in the connection pool associated with this
101 public void shutdown() {
102 if (dataSource != null) {
105 } catch (SQLException e) {
106 logger.error("Datasource cannot be closed normally.", e.getMessage());
114 * Get datasource status
118 public Map<String, Integer> getDataSourceStatus() {
119 Map<String, Integer> map = new HashMap<>(2);
120 map.put(DataSourceStatus.ACTIVE_NUMBER.getAttribute(), dataSource.getNumActive());
121 map.put(DataSourceStatus.IDLE_NUMBER.getAttribute(), dataSource.getNumIdle());
126 private BasicDataSource getBasicDataSource(String connectURI, String username, String password,
127 String driverClass, Integer initialSize, Integer maxtotal,
128 Integer maxIdle, Integer maxWaitMillis, Integer minIdle) {
129 BasicDataSource dataSource = new BasicDataSource();
130 dataSource.setDriverClassName(driverClass);
131 dataSource.setUsername(username);
132 dataSource.setPassword(password);
133 dataSource.setUrl(connectURI);
135 if (initialSize != null) {
136 dataSource.setInitialSize(initialSize);
138 if (maxtotal != null) {
139 dataSource.setMaxTotal(maxtotal);
141 if (maxIdle != null) {
142 dataSource.setMaxIdle(maxIdle);
144 if (maxWaitMillis != null) {
145 dataSource.setMaxWaitMillis(maxWaitMillis);
147 if (minIdle != null) {
148 dataSource.setMinIdle(minIdle);