From 0d015cb7b6e89ac32573fd8021e142689f82beef Mon Sep 17 00:00:00 2001 From: Ramesh Reddy Date: Fri, 24 Apr 2015 17:58:00 -0500 Subject: [PATCH] OLINGO-573: fixing the rollback logic in the case of batch changeset error --- .../olingo/server/core/ServiceHandler.java | 12 +++++++--- .../server/core/requests/BatchRequest.java | 23 +++++++++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceHandler.java b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceHandler.java index 621f0debd..2e84b400b 100644 --- a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceHandler.java +++ b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceHandler.java @@ -235,21 +235,27 @@ public interface ServiceHandler extends Processor { * During a batch operation, this method starts the transaction (if any) before any operation is handled * by the service. No nested transactions. * @return must return a unique transaction id that references a atomic operation. + * @throws ODataTranslatedException + * @throws ODataApplicationException */ - String startTransaction(); + String startTransaction() throws ODataTranslatedException, ODataApplicationException;; /** * When a batch operation is complete and all the intermediate service requests are successful, then * commit is called with transaction id returned in the startTransaction method. * @param txnId + * @throws ODataTranslatedException + * @throws ODataApplicationException */ - void commit(String txnId); + void commit(String txnId) throws ODataTranslatedException, ODataApplicationException;; /** * When a batch operation is in-complete due to an error in the middle of changeset, then rollback is * called with transaction id, that returned from startTransaction method. * @param txnId + * @throws ODataTranslatedException + * @throws ODataApplicationException */ - void rollback(String txnId); + void rollback(String txnId) throws ODataTranslatedException, ODataApplicationException;; /** * This is not complete, more URL parsing changes required. Cross join between two entities. diff --git a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/BatchRequest.java b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/BatchRequest.java index 25af023e0..0547775c8 100644 --- a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/BatchRequest.java +++ b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/BatchRequest.java @@ -77,12 +77,25 @@ public class BatchRequest extends ServiceRequest { for (BatchRequestPart part : parts) { if (part.isChangeSet()) { - String txnId = handler.startTransaction(); - partResponse = processChangeSet(part, handler); - if (partResponse.getResponses().get(0).getStatusCode() > 400) { - handler.rollback(txnId); + String txnId = null; + try { + txnId = handler.startTransaction(); + partResponse = processChangeSet(part, handler); + if (partResponse.getResponses().get(0).getStatusCode() > 400) { + handler.rollback(txnId); + } + handler.commit(txnId); + } catch(ODataTranslatedException e) { + if (txnId != null) { + handler.rollback(txnId); + } + throw e; + } catch (ODataApplicationException e) { + if (txnId != null) { + handler.rollback(txnId); + } + throw e; } - handler.commit(txnId); } else { // single request, a static request ODataRequest partRequest = part.getRequests().get(0);