]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5943: Initial experiment with running multiple SQL prepared statements wrapped...
authorAron Roberts <aron@socrates.berkeley.edu>
Sun, 7 Apr 2013 19:24:06 +0000 (12:24 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Sun, 7 Apr 2013 19:24:06 +0000 (12:24 -0700)
services/common/src/main/java/org/collectionspace/services/common/storage/JDBCTools.java

index defa2d88e782a3faf7c54b9973323d6f98b542d3..7ea50b0547ca99d0a881cfb13b8c53ce9843f206 100644 (file)
@@ -35,7 +35,9 @@ import java.sql.ResultSet;
 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
@@ -240,6 +242,62 @@ public class JDBCTools {
                 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