From: Aron Roberts Date: Mon, 8 Apr 2013 18:53:17 +0000 (-0700) Subject: CSPACE-5943: Execute multiple prepared statements, including a mix of queries and... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=40f6bb3b293c4d02e3a88726d0871be66134342e;p=tmp%2Fjakarta-migration.git CSPACE-5943: Execute multiple prepared statements, including a mix of queries and updates, within a single 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 60ee9fca9..c9c6f89e4 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 @@ -211,7 +211,7 @@ public class JDBCTools { } public static CachedRowSet executePreparedQuery(final PreparedStatementBuilder builder, - String dataSourceName, String repositoryName, String sql) throws Exception { + String dataSourceName, String repositoryName) throws Exception { Connection conn = null; PreparedStatement ps = null; try { @@ -252,7 +252,7 @@ public class JDBCTools { // 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 { + String dataSourceName, String repositoryName, Boolean executeWithinTransaction) throws Exception { Connection conn = null; PreparedStatement ps = null; List results = new ArrayList<>(); @@ -271,10 +271,17 @@ public class JDBCTools { statementCount++; logger.info("prepared statement " + statementCount + "=" + ps.toString()); } - try (ResultSet resultSet = ps.executeQuery()) { - crs.populate(resultSet); + // Try executing each statement, first as a query, then as an update + try { + ResultSet resultSet = ps.executeQuery(); + if (resultSet != null) { + crs.populate(resultSet); + results.add(crs); + } + } catch (Exception e) { + int rowcount = ps.executeUpdate(); + // Throw uncaught exception here if update attempt also fails } - results.add(crs); } return results; } catch (SQLException sqle) { diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java index 3a1fd1adb..b6f025a6d 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java @@ -85,6 +85,7 @@ import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.common.config.ConfigUtils; import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl; import org.collectionspace.services.common.config.TenantBindingUtils; +import org.collectionspace.services.common.storage.PreparedStatementBuilder; import org.collectionspace.services.config.tenant.TenantBindingType; import org.nuxeo.ecm.core.opencmis.impl.server.NuxeoCmisService; import org.nuxeo.ecm.core.opencmis.impl.server.NuxeoRepository; @@ -930,8 +931,23 @@ public class RepositoryJavaClientImpl implements RepositoryClient builders = new ArrayList<>(); + builders.add(joinControlBuilder); + builders.add(queryBuilder); String dataSourceName = JDBCTools.NUXEO_DATASOURCE_NAME; String repositoryName = ctx.getRepositoryName(); + final Boolean EXECUTE_WITHIN_TRANSACTION = true; Set docIds = new HashSet<>(); - try (CachedRowSet crs = JDBCTools.executePreparedQuery(jdbcFilterQueryBuilder, - dataSourceName, repositoryName, sql)) { + try { + List resultsList = JDBCTools.executePreparedQueries(builders, + dataSourceName, repositoryName, EXECUTE_WITHIN_TRANSACTION); - // If the response to the query is null or contains zero rows, + // One set of results are expected, from the second prepared statement executed. + // If fewer results are returned, return an empty list of document models + if (resultsList == null || resultsList.size() < 1) { + return result; + } + // Join control query will not return results, so query results will + // be the first set of results (rowSet) returned in the list + CachedRowSet queryResults = resultsList.get(0); + + // If the result from executing the query is null or contains zero rows, // return an empty list of document models - if (crs == null) { + if (queryResults == null) { return result; } - crs.last(); - if (crs.getRow() == 0) { + queryResults.last(); + if (queryResults.getRow() == 0) { return result; // empty list of document models } // Otherwise, get the document IDs from the results of the query String id; - crs.beforeFirst(); - while (crs.next()) { - id = crs.getString(1); + queryResults.beforeFirst(); + while (queryResults.next()) { + id = queryResults.getString(1); if (Tools.notBlank(id)) { docIds.add(id); } } } catch (SQLException sqle) { - logger.warn("Could not obtain document IDs via SQL query '" + sql + "': " + sqle.getMessage()); + logger.warn("Could not obtain document IDs via SQL query '" + querySql + "': " + sqle.getMessage()); return result; // return an empty list of document models }