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