View Javadoc
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      private static final Logger log = LoggerFactory.getLogger(JdbcUtils.class);
42  
43      /**
44       * Private constructor to prevent instantiation.
45       */
46      private JdbcUtils() {
47      }
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          if (connection != null) {
57              try {
58                  connection.close();
59              } catch (SQLException ex) {
60                  if (log.isDebugEnabled()) {
61                      log.debug("Could not close JDBC Connection", ex);
62                  }
63              } catch (Throwable ex) {
64                  if (log.isDebugEnabled()) {
65                      log.debug("Unexpected exception on closing JDBC Connection", ex);
66                  }
67              }
68          }
69      }
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          if (statement != null) {
79              try {
80                  statement.close();
81              } catch (SQLException ex) {
82                  if (log.isDebugEnabled()) {
83                      log.debug("Could not close JDBC Statement", ex);
84                  }
85              } catch (Throwable ex) {
86                  if (log.isDebugEnabled()) {
87                      log.debug("Unexpected exception on closing JDBC Statement", ex);
88                  }
89              }
90          }
91      }
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         if (rs != null) {
101             try {
102                 rs.close();
103             } catch (SQLException ex) {
104                 if (log.isDebugEnabled()) {
105                     log.debug("Could not close JDBC ResultSet", ex);
106                 }
107             } catch (Throwable ex) {
108                 if (log.isDebugEnabled()) {
109                     log.debug("Unexpected exception on closing JDBC ResultSet", ex);
110                 }
111             }
112         }
113     }
114 
115 }