1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
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
49
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 }