View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.jdbc;
17  
18  import java.sql.Connection;
19  import java.sql.SQLException;
20  import java.sql.Statement;
21  import java.util.List;
22  
23  import javax.sql.DataSource;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.springframework.jdbc.datasource.DataSourceUtils;
28  import org.springframework.util.Assert;
29  
30  public class JdbcUtils {
31  
32  	private static final Logger logger = LoggerFactory.getLogger(JdbcUtils.class);
33  
34  	/**
35  	 * Return the total size of the SQL statements contained in <code>sources</code>. Assumes <code>sources</code> has had its
36  	 * <code>SqlMetaData</code> filled in.
37  	 */
38  	public static long getSqlSize(List<SqlSource> sources) {
39  		long size = 0;
40  		for (SqlSource source : sources) {
41  			SqlMetaData smd = source.getMetaData();
42  			size += smd.getSize();
43  		}
44  		return size;
45  	}
46  
47  	/**
48  	 * Return a count of the total number of SQL statements contained in <code>sources</code>. Assumes <code>sources</code> has had its
49  	 * <code>SqlMetaData</code> filled in.
50  	 */
51  	public static long getSqlCount(List<SqlSource> sources) {
52  		long count = 0;
53  		for (SqlSource source : sources) {
54  			SqlMetaData smd = source.getMetaData();
55  			count += smd.getCount();
56  		}
57  		return count;
58  	}
59  
60  	public static final void closeQuietly(DataSource dataSource, Connection conn, Statement statement) {
61  		closeQuietly(statement);
62  		closeQuietly(dataSource, conn);
63  	}
64  
65  	public static final void closeQuietly(Statement statement) {
66  		if (statement == null) {
67  			return;
68  		}
69  		try {
70  			statement.close();
71  		} catch (SQLException e) {
72  			throw new JdbcException(e);
73  		}
74  	}
75  
76  	public static final void closeQuietly(DataSource dataSource, Connection conn) {
77  		if (conn == null && dataSource == null) {
78  			return;
79  		}
80  		Assert.notNull(dataSource, "dataSource is null but conn is not");
81  		try {
82  			logger.trace("releasing connection");
83  			DataSourceUtils.doReleaseConnection(conn, dataSource);
84  		} catch (SQLException e) {
85  			throw new JdbcException(e);
86  		}
87  	}
88  
89  }