From: Aron Roberts Date: Sun, 7 Apr 2013 19:24:06 +0000 (-0700) Subject: CSPACE-5943: Initial experiment with running multiple SQL prepared statements wrapped... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=074920b835b7256d4d524b3cfd28afc0aebd34f6;p=tmp%2Fjakarta-migration.git CSPACE-5943: Initial experiment with running multiple SQL prepared statements wrapped inside a transaction. --- diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java index defa2d88e..7ea50b054 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java @@ -35,7 +35,9 @@ import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import javax.sql.rowset.CachedRowSet; import javax.sql.rowset.RowSetFactory; import javax.sql.rowset.RowSetProvider; @@ -240,6 +242,62 @@ public class JDBCTools { if (conn != null) { conn.close(); } + } catch (SQLException sqle) { + logger.debug("SQL Exception closing statement/connection in executePreparedQuery: " + sqle.getLocalizedMessage()); + return null; + } + } + } + + // FIXME: This method's code significantly overlaps that of executePrepareQuery(), above, + // and the two could be refactored into a single method, if desired. + public static List executePreparedQueries(final List builders, + String dataSourceName, String repositoryName, String sql, Boolean executeWithinTransaction) throws Exception { + Connection conn = null; + PreparedStatement ps = null; + List results = new ArrayList<>(); + try { + conn = getConnection(dataSourceName, repositoryName); + if (executeWithinTransaction) { + conn.setAutoCommit(false); + } + RowSetFactory rowSetFactory = RowSetProvider.newFactory(); + CachedRowSet crs = rowSetFactory.createCachedRowSet(); + int statementCount = 0; + for (PreparedStatementBuilder builder : builders) { + ps = builder.build(conn); + // FIXME: transition this log statement to DEBUG level when appropriate + if (logger.isInfoEnabled()) { + statementCount++; + logger.info("prepared statement " + statementCount + "=" + ps.toString()); + } + try (ResultSet resultSet = ps.executeQuery()) { + crs.populate(resultSet); + } + results.add(crs); + } + return results; + } catch (SQLException sqle) { + if (executeWithinTransaction && conn != null) { + conn.rollback(); + } + SQLException tempException = sqle; + while (null != tempException) { // SQLExceptions can be chained. Loop to log all. + logger.debug("SQL Exception: " + sqle.getLocalizedMessage()); + tempException = tempException.getNextException(); + } + throw new RuntimeException("SQL problem in executePreparedQuery: ", sqle); + } finally { + try { + if (ps != null) { + ps.close(); + } + if (conn != null) { + if (executeWithinTransaction) { + conn.commit(); + } + conn.close(); + } } catch (SQLException sqle) { logger.debug("SQL Exception closing statement/connection in executeQuery: " + sqle.getLocalizedMessage()); return null;