2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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,
66 String driverClass, Integer initialSize, Integer maxActive,
67 Integer maxIdle, Integer maxWait, Integer minIdle) {
68 this.dataSource = getBasicDataSource(connectURI, username, password, driverClass,
69 initialSize, maxActive, maxIdle, maxWait, minIdle);
73 * Get a connection from datasource which is thread safe.
77 public Connection getConnection() throws DBConnectionPoolException {
78 if (dataSource == null) {
79 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 private 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);