2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.dao.util.dbcp;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.apache.commons.dbcp2.BasicDataSource;
30 import org.onap.appc.dao.util.api.DBConnectionPoolService;
31 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
33 import java.sql.Connection;
34 import java.sql.SQLException;
35 import java.util.HashMap;
39 * This class provides ability to create database connection pool.
41 public class DBConnectionPool implements DBConnectionPoolService {
43 private final EELFLogger logger = EELFManager.getInstance().getLogger(DBConnectionPool.class);
44 private BasicDataSource dataSource;
46 public enum DataSourceStatus {
47 ACTIVE_NUMBER("active_number"),
48 IDLE_NUMBER("idle_number");
50 private String attribute;
52 DataSourceStatus(String attribute) {
53 this.attribute = attribute;
56 public String getAttribute() {
61 public DBConnectionPool(String connectURI, String username, String password, String driverClass) {
62 this(connectURI, username, password, driverClass, null, null, null, null, null);
65 public DBConnectionPool(String connectURI, String username, String password, String driverClass,
66 Integer initialSize, Integer maxActive, 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();
80 Connection connection = null;
82 connection = dataSource.getConnection();
83 } catch (SQLException e) {
84 logger.error("Get connection failure", e);
85 throw new DBConnectionPoolException(e);
88 if (connection == null) {
89 // Don't necessarily know why, but can report. . . .
90 throw new DBConnectionPoolException("Connection was not created");
97 * Closes and releases all idle connections that are currently stored in the connection pool associated with this
100 public void shutdown() {
101 if (dataSource != null) {
104 } catch (SQLException e) {
105 logger.error("Datasource cannot be closed normally.", e.getMessage());
113 * Get datasource status
117 public Map<String, Integer> getDataSourceStatus() {
118 Map<String, Integer> map = new HashMap<>(2);
119 map.put(DataSourceStatus.ACTIVE_NUMBER.getAttribute(), dataSource.getNumActive());
120 map.put(DataSourceStatus.IDLE_NUMBER.getAttribute(), dataSource.getNumIdle());
125 protected BasicDataSource getBasicDataSource(String connectURI, String username, String password,
126 String driverClass, Integer initialSize, Integer maxTotal, Integer maxIdle, Integer maxWaitMillis,
128 BasicDataSource dataSource = new BasicDataSource();
129 dataSource.setDriverClassName(driverClass);
130 dataSource.setUsername(username);
131 dataSource.setPassword(password);
132 dataSource.setUrl(connectURI);
134 if (initialSize != null) {
135 dataSource.setInitialSize(initialSize);
137 if (maxTotal != null) {
138 dataSource.setMaxTotal(maxTotal);
140 if (maxIdle != null) {
141 dataSource.setMaxIdle(maxIdle);
143 if (maxWaitMillis != null) {
144 dataSource.setMaxWaitMillis(maxWaitMillis);
146 if (minIdle != null) {
147 dataSource.setMinIdle(minIdle);