001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019 package org.apache.shiro.util;
020
021 import java.sql.Connection;
022 import java.sql.ResultSet;
023 import java.sql.SQLException;
024 import java.sql.Statement;
025
026 import org.slf4j.Logger;
027 import org.slf4j.LoggerFactory;
028
029 /**
030 * A set of static helper methods for managing JDBC API objects.
031 * <p/>
032 * <em>Note:</em> Some parts of this class were copied from the Spring Framework and then modified.
033 * They were copied here to prevent Spring dependencies in the Shiro core API. The original license conditions
034 * (Apache 2.0) have been maintained.
035 *
036 * @author Jeremy Haile
037 * @since 0.2
038 */
039 public class JdbcUtils {
040
041 /** Private internal log instance. */
042 private static final Logger log = LoggerFactory.getLogger(JdbcUtils.class);
043
044 /**
045 * Private constructor to prevent instantiation.
046 */
047 private JdbcUtils() {
048 }
049
050 /**
051 * Close the given JDBC Connection and ignore any thrown exception.
052 * This is useful for typical finally blocks in manual JDBC code.
053 *
054 * @param connection the JDBC Connection to close (may be <tt>null</tt>)
055 */
056 public static void closeConnection(Connection connection) {
057 if (connection != null) {
058 try {
059 connection.close();
060 } catch (SQLException ex) {
061 if (log.isDebugEnabled()) {
062 log.debug("Could not close JDBC Connection", ex);
063 }
064 } catch (Throwable ex) {
065 if (log.isDebugEnabled()) {
066 log.debug("Unexpected exception on closing JDBC Connection", ex);
067 }
068 }
069 }
070 }
071
072 /**
073 * Close the given JDBC Statement and ignore any thrown exception.
074 * This is useful for typical finally blocks in manual JDBC code.
075 *
076 * @param statement the JDBC Statement to close (may be <tt>null</tt>)
077 */
078 public static void closeStatement(Statement statement) {
079 if (statement != null) {
080 try {
081 statement.close();
082 } catch (SQLException ex) {
083 if (log.isDebugEnabled()) {
084 log.debug("Could not close JDBC Statement", ex);
085 }
086 } catch (Throwable ex) {
087 if (log.isDebugEnabled()) {
088 log.debug("Unexpected exception on closing JDBC Statement", ex);
089 }
090 }
091 }
092 }
093
094 /**
095 * Close the given JDBC ResultSet and ignore any thrown exception.
096 * This is useful for typical finally blocks in manual JDBC code.
097 *
098 * @param rs the JDBC ResultSet to close (may be <tt>null</tt>)
099 */
100 public static void closeResultSet(ResultSet rs) {
101 if (rs != null) {
102 try {
103 rs.close();
104 } catch (SQLException ex) {
105 if (log.isDebugEnabled()) {
106 log.debug("Could not close JDBC ResultSet", ex);
107 }
108 } catch (Throwable ex) {
109 if (log.isDebugEnabled()) {
110 log.debug("Unexpected exception on closing JDBC ResultSet", ex);
111 }
112 }
113 }
114 }
115
116 }