import java.sql.ResultSetMetaData;\r
import java.sql.SQLException;\r
import java.sql.Statement;\r
+import java.util.ArrayList;\r
import java.util.HashMap;\r
+import java.util.List;\r
import javax.sql.rowset.CachedRowSet;\r
import javax.sql.rowset.RowSetFactory;\r
import javax.sql.rowset.RowSetProvider;\r
if (conn != null) {\r
conn.close();\r
}\r
+ } catch (SQLException sqle) {\r
+ logger.debug("SQL Exception closing statement/connection in executePreparedQuery: " + sqle.getLocalizedMessage());\r
+ return null;\r
+ }\r
+ }\r
+ }\r
+ \r
+ // FIXME: This method's code significantly overlaps that of executePrepareQuery(), above,\r
+ // and the two could be refactored into a single method, if desired.\r
+ public static List<CachedRowSet> executePreparedQueries(final List<PreparedStatementBuilder> builders,\r
+ String dataSourceName, String repositoryName, String sql, Boolean executeWithinTransaction) throws Exception {\r
+ Connection conn = null;\r
+ PreparedStatement ps = null;\r
+ List<CachedRowSet> results = new ArrayList<>();\r
+ try {\r
+ conn = getConnection(dataSourceName, repositoryName);\r
+ if (executeWithinTransaction) {\r
+ conn.setAutoCommit(false);\r
+ }\r
+ RowSetFactory rowSetFactory = RowSetProvider.newFactory();\r
+ CachedRowSet crs = rowSetFactory.createCachedRowSet();\r
+ int statementCount = 0;\r
+ for (PreparedStatementBuilder builder : builders) {\r
+ ps = builder.build(conn);\r
+ // FIXME: transition this log statement to DEBUG level when appropriate\r
+ if (logger.isInfoEnabled()) {\r
+ statementCount++;\r
+ logger.info("prepared statement " + statementCount + "=" + ps.toString());\r
+ }\r
+ try (ResultSet resultSet = ps.executeQuery()) {\r
+ crs.populate(resultSet);\r
+ }\r
+ results.add(crs);\r
+ }\r
+ return results;\r
+ } catch (SQLException sqle) {\r
+ if (executeWithinTransaction && conn != null) {\r
+ conn.rollback();\r
+ }\r
+ SQLException tempException = sqle;\r
+ while (null != tempException) { // SQLExceptions can be chained. Loop to log all.\r
+ logger.debug("SQL Exception: " + sqle.getLocalizedMessage());\r
+ tempException = tempException.getNextException();\r
+ }\r
+ throw new RuntimeException("SQL problem in executePreparedQuery: ", sqle);\r
+ } finally {\r
+ try {\r
+ if (ps != null) {\r
+ ps.close();\r
+ }\r
+ if (conn != null) {\r
+ if (executeWithinTransaction) {\r
+ conn.commit();\r
+ }\r
+ conn.close();\r
+ }\r
} catch (SQLException sqle) {\r
logger.debug("SQL Exception closing statement/connection in executeQuery: " + sqle.getLocalizedMessage());\r
return null;\r