Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-dispatcher / appc-dispatcher-common / appc-data-access-lib / src / main / java / org / onap / appc / dao / util / helper / DBHelper.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.helper;
26
27 import java.sql.Connection;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.sql.Statement;
31
32 /**
33  * This class provides the basic utility methods of database connection
34  * <p>
35  * Since currently this class only contains stateless methods, the package
36  * is exported by maven-bundle-plugin. If you have to add some stateful methods
37  * in this class, one suggested solution is that use blueprint to create a singleton
38  * which is exported as service
39  */
40 public class DBHelper {
41     /**
42      * Closes a database resultSet,statement and connection in that order. Data access objects should call this method
43      * in a finally block.
44      *
45      * @param resultSet  or null
46      * @param statement  or null
47      * @param connection or null
48      */
49     public static void close(ResultSet resultSet, Statement statement, Connection connection) {
50         try {
51             closeResultSet(resultSet);
52         } finally {
53             try {
54                 closeStatement(statement);
55             } finally {
56                 closeConnection(connection);
57             }
58         }
59     }
60
61     /**
62      * Closes a database result set. Data access objects should call this method
63      * when a result set is no longer needed.
64      *
65      * @param rs A ResultSet Object
66      */
67     public static void closeResultSet(ResultSet rs) {
68         try {
69             if (rs != null) {
70                 rs.close();
71             }
72         } catch (SQLException se) {
73             // Ignore this exception and allow execution to continue.
74             // so that connection can try to be close.
75         }
76     }
77
78     /**
79      * Closes a database query statement. Data access objects should call this
80      * method when a statement is no longer needed.
81      *
82      * @param stmt A Statement Object
83      */
84     public static void closeStatement(Statement stmt) {
85         try {
86             if (stmt != null) {
87                 stmt.close();
88             }
89         } catch (SQLException se) {
90             // Ignore this exception and allow execution to continue.
91             // so that connection can try to be close.
92         }
93     }
94
95     /**
96      * Closes a database connection. Data access objects should call this method
97      * when a database connection is no longer needed.
98      *
99      * @param connection A Connection Object     *
100      */
101     public static void closeConnection(Connection connection) {
102         try {
103             if (connection != null && !connection.isClosed()) {
104                 connection.close();
105             }
106         } catch (SQLException se) {
107             // Ignore this exception and allow execution to continue.
108         }
109     }
110 }