001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.jdbc.service;
017    
018    import java.sql.Connection;
019    import java.sql.ResultSet;
020    import java.sql.SQLException;
021    import java.sql.Statement;
022    
023    import javax.sql.DataSource;
024    
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    import org.springframework.jdbc.datasource.DataSourceUtils;
028    import org.springframework.util.Assert;
029    
030    public class JdbcUtils {
031    
032            private static final Logger logger = LoggerFactory.getLogger(JdbcUtils.class);
033    
034            public static final void closeQuietly(DataSource dataSource, Connection conn, Statement statement) {
035                    closeQuietly(statement);
036                    closeQuietly(dataSource, conn);
037            }
038    
039            public static final void closeQuietly(Statement statement) {
040                    if (statement == null) {
041                            return;
042                    }
043                    try {
044                            statement.close();
045                    } catch (SQLException e) {
046                            throw new IllegalStateException(e);
047                    }
048            }
049    
050            public static final void closeQuietly(ResultSet rs) {
051                    if (rs == null) {
052                            return;
053                    }
054                    try {
055                            rs.close();
056                    } catch (SQLException e) {
057                            throw new IllegalStateException(e);
058                    }
059            }
060    
061            public static final void closeQuietly(DataSource dataSource, Connection conn) {
062                    if (conn == null && dataSource == null) {
063                            return;
064                    }
065                    Assert.notNull(dataSource, "dataSource is null but conn is not");
066                    try {
067                            logger.debug("releasing connection");
068                            DataSourceUtils.doReleaseConnection(conn, dataSource);
069                    } catch (SQLException e) {
070                            throw new IllegalStateException(e);
071                    }
072            }
073    
074    }