From b7c8d78c9e715fdc849277d14235d6b64f4d5b9e Mon Sep 17 00:00:00 2001
From: "Kajur, Harish (vk250x)" <vk250x@att.com>
Date: Mon, 18 Feb 2019 00:45:57 -0500
Subject: Update the bulk single transaction code

to fix the memory leak due to not properly
rolling back when there is an exception
in the code

Issue-ID: AAI-2168
Change-Id: Iaea6b1942547ae9da94b70cf29bc7a405ddabbea
Signed-off-by: Kajur, Harish (vk250x) <vk250x@att.com>
---
 .../rest/bulk/BulkSingleTransactionConsumer.java   | 34 +++++++++++++++-------
 1 file changed, 23 insertions(+), 11 deletions(-)

(limited to 'aai-resources/src/main/java/org')

diff --git a/aai-resources/src/main/java/org/onap/aai/rest/bulk/BulkSingleTransactionConsumer.java b/aai-resources/src/main/java/org/onap/aai/rest/bulk/BulkSingleTransactionConsumer.java
index 674c1b43..b06abaa3 100644
--- a/aai-resources/src/main/java/org/onap/aai/rest/bulk/BulkSingleTransactionConsumer.java
+++ b/aai-resources/src/main/java/org/onap/aai/rest/bulk/BulkSingleTransactionConsumer.java
@@ -19,6 +19,8 @@
  */
 package org.onap.aai.rest.bulk;
 
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import org.javatuples.Pair;
@@ -58,7 +60,8 @@ public class BulkSingleTransactionConsumer extends RESTAPI {
 	private static final String TARGET_ENTITY = "aai-resources";
 	private static final Set<String> validOperations = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("put", "patch", "delete")));
 	private int allowedOperationCount = 30;
-	private TransactionalGraphEngine dbEngine = null;
+
+	private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(BulkSingleTransactionConsumer.class);
 
 	@POST
 	@Consumes(value = javax.ws.rs.core.MediaType.APPLICATION_JSON)
@@ -72,6 +75,11 @@ public class BulkSingleTransactionConsumer extends RESTAPI {
 		DBConnectionType type;
 
 		initLogging(req, transId, sourceOfTruth);
+		boolean success = true;
+
+		TransactionalGraphEngine dbEngine = null;
+		TransactionResponse transactionResponse;
+		Response response;
 
 		try {
 			if(AAIConfig.get("aai.use.realtime", "true").equals("true")){
@@ -101,7 +109,7 @@ public class BulkSingleTransactionConsumer extends RESTAPI {
 			HttpEntry resourceHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class);
 			resourceHttpEntry.setHttpEntryProperties(version, type);
 			Loader loader = resourceHttpEntry.getLoader();
-			TransactionalGraphEngine dbEngine = resourceHttpEntry.getDbEngine();
+			dbEngine = resourceHttpEntry.getDbEngine();
 
 			//populate uri query
 			populateUriQuery(bulkOperations, dbEngine);
@@ -116,28 +124,32 @@ public class BulkSingleTransactionConsumer extends RESTAPI {
 			Pair<Boolean, List<Pair<URI, Response>>> results = resourceHttpEntry.process(dbRequests, sourceOfTruth, this.enableResourceVersion());
 
 			//process result of db requests
-			TransactionResponse transactionResponse = buildTransactionResponse(transaction, results.getValue1());
+			transactionResponse = buildTransactionResponse(transaction, results.getValue1());
 
 			//commit/rollback based on results
-			if (results.getValue0()) { //everything was processed without error
-				dbEngine.commit();
-			} else { //something failed
-				dbEngine.rollback();
-			}
+			success = results.getValue0();
 
-			return Response
+			response = Response
 					.status(Response.Status.CREATED)
 					.entity(new GsonBuilder().serializeNulls().create().toJson(transactionResponse))
 					.build();
 
 		} catch (AAIException e) {
-			return consumerExceptionResponseGenerator(headers, info, javax.ws.rs.HttpMethod.POST, e);
+			response = consumerExceptionResponseGenerator(headers, info, javax.ws.rs.HttpMethod.POST, e);
+			success  = false;
 		} finally {
 			if (dbEngine != null) {
-				dbEngine.rollback();
+				if(success){
+					dbEngine.commit();
+					LOGGER.info("Successfully committed the transaction to the database");
+				} else {
+					dbEngine.rollback();
+					LOGGER.info("Rolled back the transaction due to failure");
+				}
 			}
 		}
 
+		return response;
 	}
 
 
-- 
cgit