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