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     * @since 0.2
037     */
038    public class JdbcUtils {
039    
040        /** Private internal log instance. */
041        private static final Logger log = LoggerFactory.getLogger(JdbcUtils.class);
042    
043        /**
044         * Private constructor to prevent instantiation.
045         */
046        private JdbcUtils() {
047        }
048    
049        /**
050         * Close the given JDBC Connection and ignore any thrown exception.
051         * This is useful for typical finally blocks in manual JDBC code.
052         *
053         * @param connection the JDBC Connection to close (may be <tt>null</tt>)
054         */
055        public static void closeConnection(Connection connection) {
056            if (connection != null) {
057                try {
058                    connection.close();
059                } catch (SQLException ex) {
060                    if (log.isDebugEnabled()) {
061                        log.debug("Could not close JDBC Connection", ex);
062                    }
063                } catch (Throwable ex) {
064                    if (log.isDebugEnabled()) {
065                        log.debug("Unexpected exception on closing JDBC Connection", ex);
066                    }
067                }
068            }
069        }
070    
071        /**
072         * Close the given JDBC Statement and ignore any thrown exception.
073         * This is useful for typical finally blocks in manual JDBC code.
074         *
075         * @param statement the JDBC Statement to close (may be <tt>null</tt>)
076         */
077        public static void closeStatement(Statement statement) {
078            if (statement != null) {
079                try {
080                    statement.close();
081                } catch (SQLException ex) {
082                    if (log.isDebugEnabled()) {
083                        log.debug("Could not close JDBC Statement", ex);
084                    }
085                } catch (Throwable ex) {
086                    if (log.isDebugEnabled()) {
087                        log.debug("Unexpected exception on closing JDBC Statement", ex);
088                    }
089                }
090            }
091        }
092    
093        /**
094         * Close the given JDBC ResultSet and ignore any thrown exception.
095         * This is useful for typical finally blocks in manual JDBC code.
096         *
097         * @param rs the JDBC ResultSet to close (may be <tt>null</tt>)
098         */
099        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    }