2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications Copyright (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.dao.util.dbcp;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.apache.commons.dbcp2.BasicDataSource;
31 import org.onap.appc.dao.util.api.DBConnectionPoolService;
32 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
34 import java.sql.Connection;
35 import java.sql.SQLException;
36 import java.util.HashMap;
40 * This class provides ability to create database connection pool.
42 public class DBConnectionPool implements DBConnectionPoolService {
44 private final EELFLogger logger = EELFManager.getInstance().getLogger(DBConnectionPool.class);
45 private BasicDataSource dataSource;
47 public enum DataSourceStatus {
48 ACTIVE_NUMBER("active_number"),
49 IDLE_NUMBER("idle_number");
51 private String attribute;
53 DataSourceStatus(String attribute) {
54 this.attribute = attribute;
57 public String getAttribute() {
62 public DBConnectionPool(String connectURI, String username, String password, String driverClass) {
63 this(connectURI, username, password, driverClass, null, null, null, null, null);
66 public DBConnectionPool(String connectURI, String username, String password,
67 String driverClass, Integer initialSize, Integer maxActive,
68 Integer maxIdle, Integer maxWait, Integer minIdle) {
69 this.dataSource = getBasicDataSource(connectURI, username, password, driverClass,
70 initialSize, maxActive, maxIdle, maxWait, minIdle);
74 * Get a connection from datasource which is thread safe.
78 public Connection getConnection() throws DBConnectionPoolException {
79 if (dataSource == null) {
80 throw new DBConnectionPoolException();
82 Connection connection = null;
84 connection = dataSource.getConnection();
85 } catch (SQLException e) {
86 logger.error("Get connection failure", e);
87 throw new DBConnectionPoolException(e);
90 if(connection == null){
92 throw new DBConnectionPoolException("Connection was not created");
99 * Closes and releases all idle connections that are currently stored in the connection pool associated with this
102 public void shutdown() {
103 if (dataSource != null) {
106 } catch (SQLException e) {
107 logger.error("Datasource cannot be closed normally.", e.getMessage());
115 * Get datasource status
119 public Map<String, Integer> getDataSourceStatus() {
120 Map<String, Integer> map = new HashMap<>(2);
121 map.put(DataSourceStatus.ACTIVE_NUMBER.getAttribute(), dataSource.getNumActive());
122 map.put(DataSourceStatus.IDLE_NUMBER.getAttribute(), dataSource.getNumIdle());
127 protected BasicDataSource getBasicDataSource(String connectURI, String username, String password,
128 String driverClass, Integer initialSize, Integer maxtotal,
129 Integer maxIdle, Integer maxWaitMillis, Integer minIdle) {
130 BasicDataSource dataSource = new BasicDataSource();
131 dataSource.setDriverClassName(driverClass);
132 dataSource.setUsername(username);
133 dataSource.setPassword(password);
134 dataSource.setUrl(connectURI);
136 if (initialSize != null) {
137 dataSource.setInitialSize(initialSize);
139 if (maxtotal != null) {
140 dataSource.setMaxTotal(maxtotal);
142 if (maxIdle != null) {
143 dataSource.setMaxIdle(maxIdle);
145 if (maxWaitMillis != null) {
146 dataSource.setMaxWaitMillis(maxWaitMillis);
148 if (minIdle != null) {
149 dataSource.setMinIdle(minIdle);