6f51fec3c54a02746eea996d0377c0a9ca6d9890
[appc.git] / appc-dispatcher / appc-dispatcher-common / appc-data-access-lib / src / main / java / org / onap / appc / dao / util / api / DBConnectionPoolService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications (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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.dao.util.api;
27
28 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
29
30 import java.sql.Connection;
31 import java.util.Map;
32
33 /**
34  * This class is the interface of DBConnectionPool.
35  * <p>
36  * Below is an example of how to query an entry from database.
37  * Inject AppcDatabaseConnectionPool bean by the blueprint first
38  * for example,
39  * {@code
40  * <reference id="AppcMysqlDBConnectionPoolService" availability="mandatory"
41  * activation="eager" interface="org.onap.appc.dao.util.api.DBConnectionPoolService" />
42  * }
43  * <p>
44  * Then, query the data and close ResultSet, Statement, Connection.
45  * <blockquote><pre>
46  * {@code
47  * private AppcDatabaseConnectionPool pool;
48  * public void setAppcDatabaseConnectionPool(AppcDatabaseConnectionPool pool){
49  *     this.pool = pool;
50  * }
51  * public queryAppcDatabase(AppcDatabaseConnectionPool pool){
52  *      Connection connection = null;
53  *      try {
54  *          connection = pool.getConnection();
55  *      } catch (DBConnectionPoolException e) {
56  *          e.printStackTrace();
57  *      }
58  *      Connection conn = null;
59  *      Statement stmt = null;
60  *      ResultSet rs = null;
61  *      try {
62  *            stmt = connection.createStatement();
63  *            rs = stmt.executeQuery("select * from appcctl.transactions");
64  *            System.out.println("# of entries in db:");
65  *            int numcols = rs.getMetaData().getColumnCount();
66  *            System.out.println(pool.getDataSourceStatus());
67  *      }catch (SQLException e) {
68  *          e.printStackTrace();
69  *      } finally {
70  *          try {
71  *               pool.close(rs, stmt, conn);
72  *              } catch (DataAccessException e) {
73  *                  e.printStackTrace();
74  *              }
75  *     }
76  * }
77  * }
78  * <p>
79  * </pre></blockquote>
80  */
81 public interface DBConnectionPoolService {
82     /**
83      * Get a jdbc connection
84      *
85      * @return connection {@link Connection}
86      * @throws DBConnectionPoolException - if a {@link Connection} cannot be return.
87      */
88     Connection getConnection() throws DBConnectionPoolException;
89
90     /**
91      * Get Data source status
92      *
93      * @return map
94      */
95     Map<String, Integer> getDataSourceStatus();
96
97 }