• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos.devtools.datasources.utils.common.util;
17 
18 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
19 import org.apache.logging.log4j.LogManager;
20 import org.apache.logging.log4j.Logger;
21 
22 import java.sql.Connection;
23 import java.sql.PreparedStatement;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 
27 /**
28  * Close Resource Util
29  *
30  * @since 2021/5/19 16:39
31  */
32 public class CloseResourceUtil {
33     private static final Logger LOGGER = LogManager.getLogger(CloseResourceUtil.class);
34 
35     /**
36      * Close Resource
37      *
38      * @param logger Logger
39      * @param conn Connection
40      * @param ps PreparedStatement
41      * @param statement Statement
42      */
closeResource(Logger logger, Connection conn, PreparedStatement ps, Statement statement)43     public static void closeResource(Logger logger, Connection conn, PreparedStatement ps, Statement statement) {
44         if (ProfilerLogManager.isInfoEnabled()) {
45             LOGGER.info("closeResource");
46         }
47         if (ps != null) {
48             try {
49                 ps.close();
50             } catch (SQLException exception) {
51                 if (ProfilerLogManager.isErrorEnabled()) {
52                     LOGGER.error("SQLException error: " + exception.getMessage());
53                 }
54             }
55         }
56 
57         if (conn != null) {
58             try {
59                 conn.close();
60             } catch (SQLException exception) {
61                 if (ProfilerLogManager.isErrorEnabled()) {
62                     LOGGER.error("SQLException error: " + exception.getMessage());
63                 }
64             }
65         }
66 
67         if (statement != null) {
68             try {
69                 statement.close();
70             } catch (SQLException exception) {
71                 if (ProfilerLogManager.isErrorEnabled()) {
72                     LOGGER.error("SQLException error: " + exception.getMessage());
73                 }
74             }
75         }
76     }
77 }
78