Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
JdbcUtils |
|
| 4.75;4.75 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | package org.apache.shiro.util; | |
20 | ||
21 | import java.sql.Connection; | |
22 | import java.sql.ResultSet; | |
23 | import java.sql.SQLException; | |
24 | import java.sql.Statement; | |
25 | ||
26 | import org.slf4j.Logger; | |
27 | import org.slf4j.LoggerFactory; | |
28 | ||
29 | /** | |
30 | * A set of static helper methods for managing JDBC API objects. | |
31 | * <p/> | |
32 | * <em>Note:</em> Some parts of this class were copied from the Spring Framework and then modified. | |
33 | * They were copied here to prevent Spring dependencies in the Shiro core API. The original license conditions | |
34 | * (Apache 2.0) have been maintained. | |
35 | * | |
36 | * @since 0.2 | |
37 | */ | |
38 | public class JdbcUtils { | |
39 | ||
40 | /** Private internal log instance. */ | |
41 | 1 | private static final Logger log = LoggerFactory.getLogger(JdbcUtils.class); |
42 | ||
43 | /** | |
44 | * Private constructor to prevent instantiation. | |
45 | */ | |
46 | 0 | private JdbcUtils() { |
47 | 0 | } |
48 | ||
49 | /** | |
50 | * Close the given JDBC Connection and ignore any thrown exception. | |
51 | * This is useful for typical finally blocks in manual JDBC code. | |
52 | * | |
53 | * @param connection the JDBC Connection to close (may be <tt>null</tt>) | |
54 | */ | |
55 | public static void closeConnection(Connection connection) { | |
56 | 48 | if (connection != null) { |
57 | try { | |
58 | 37 | connection.close(); |
59 | 0 | } catch (SQLException ex) { |
60 | 0 | if (log.isDebugEnabled()) { |
61 | 0 | log.debug("Could not close JDBC Connection", ex); |
62 | } | |
63 | 0 | } catch (Throwable ex) { |
64 | 0 | if (log.isDebugEnabled()) { |
65 | 0 | log.debug("Unexpected exception on closing JDBC Connection", ex); |
66 | } | |
67 | 37 | } |
68 | } | |
69 | 48 | } |
70 | ||
71 | /** | |
72 | * Close the given JDBC Statement and ignore any thrown exception. | |
73 | * This is useful for typical finally blocks in manual JDBC code. | |
74 | * | |
75 | * @param statement the JDBC Statement to close (may be <tt>null</tt>) | |
76 | */ | |
77 | public static void closeStatement(Statement statement) { | |
78 | 50 | if (statement != null) { |
79 | try { | |
80 | 39 | statement.close(); |
81 | 0 | } catch (SQLException ex) { |
82 | 0 | if (log.isDebugEnabled()) { |
83 | 0 | log.debug("Could not close JDBC Statement", ex); |
84 | } | |
85 | 0 | } catch (Throwable ex) { |
86 | 0 | if (log.isDebugEnabled()) { |
87 | 0 | log.debug("Unexpected exception on closing JDBC Statement", ex); |
88 | } | |
89 | 39 | } |
90 | } | |
91 | 50 | } |
92 | ||
93 | /** | |
94 | * Close the given JDBC ResultSet and ignore any thrown exception. | |
95 | * This is useful for typical finally blocks in manual JDBC code. | |
96 | * | |
97 | * @param rs the JDBC ResultSet to close (may be <tt>null</tt>) | |
98 | */ | |
99 | public static void closeResultSet(ResultSet rs) { | |
100 | 17 | if (rs != null) { |
101 | try { | |
102 | 17 | rs.close(); |
103 | 0 | } catch (SQLException ex) { |
104 | 0 | if (log.isDebugEnabled()) { |
105 | 0 | log.debug("Could not close JDBC ResultSet", ex); |
106 | } | |
107 | 0 | } catch (Throwable ex) { |
108 | 0 | if (log.isDebugEnabled()) { |
109 | 0 | log.debug("Unexpected exception on closing JDBC ResultSet", ex); |
110 | } | |
111 | 17 | } |
112 | } | |
113 | 17 | } |
114 | ||
115 | } |