From faf965656265dd96a350ce03bb478ef2b9a3805b Mon Sep 17 00:00:00 2001 From: "k.kedron" Date: Wed, 21 Jul 2021 15:02:02 +0200 Subject: WO for MariaDB transaction creation issue Add retry mechanism Issue-ID: CCSDK-3381 Signed-off-by: Krystian Kedron Change-Id: Ib47939d979683ef25155d40ac0e2dd3fb609af4b --- .../ms/neng/core/service/rs/RestServiceImpl.java | 49 +++++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/rs/RestServiceImpl.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/rs/RestServiceImpl.java index 95afea21..893808ec 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/rs/RestServiceImpl.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/rs/RestServiceImpl.java @@ -35,6 +35,7 @@ import org.onap.ccsdk.apps.ms.neng.core.service.SpringService; import org.onap.ccsdk.apps.ms.neng.persistence.entity.PolicyDetails; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.springframework.transaction.CannotCreateTransactionException; import org.springframework.web.bind.annotation.RequestBody; /** @@ -42,11 +43,15 @@ import org.springframework.web.bind.annotation.RequestBody; */ @Component public class RestServiceImpl implements RestService { - private static Logger log = Logger.getLogger(RestServiceImpl.class.getName()); - private static final String INTERNAL_ERROR_MSG = "Internal error occured while processing the request."; + + private static final Logger log = Logger.getLogger(RestServiceImpl.class.getName()); + private static final String INTERNAL_ERROR_MSG = "Internal error occurred while processing the request: "; + private static final String JDBC_CONNECTION_ERROR_MSG = "Error during JDBC transaction creation: "; private static final String ERROR="error"; private static final String ERROR_500="err-0500"; - + private static final int MAX_RETRY = 5; + private int retry = 0; + @Autowired SpringService service; /** @@ -65,18 +70,18 @@ public class RestServiceImpl implements RestService { @Override public Response generateNetworkElementName(@RequestBody @Valid NameGenRequest request) { log.info("Received request: " + request.toString()); - Map response = new HashMap<>(); try { NameGenResponse resp = service.generateOrUpdateName(request); return buildResponse(resp); } catch (NengException e) { log.warning(e.getMessage()); - response.put(ERROR, buildErrorResponse("NELGEN-0003", e.getMessage())); - return buildErrorResponse(response); + return handleException("NELGEN-0003", e.getMessage()); + } catch (CannotCreateTransactionException e) { + log.warning(e.getMessage()); + return handleJDBCConnectionException(request, e); } catch (Exception e) { log.warning(e.getMessage()); - response.put(ERROR, buildErrorResponse(ERROR_500, INTERNAL_ERROR_MSG)); - return buildErrorResponse(response); + return handleException(ERROR_500, INTERNAL_ERROR_MSG); } } @@ -129,15 +134,37 @@ public class RestServiceImpl implements RestService { } } - Response buildResponse(Object response) { + private Response handleException(String statusCode, String statusMessage) { + Map response = new HashMap<>(); + response.put(ERROR, buildErrorResponse(statusCode, statusMessage)); + + return buildErrorResponse(response); + } + + private Response handleJDBCConnectionException(NameGenRequest request, CannotCreateTransactionException e) { + retry += 1; + if (retry <= MAX_RETRY) { + log.info("Try to generate network element name again! Attempt: " + retry); + Response response = generateNetworkElementName(request); + if (response.getStatus() != 200) { + retry = 0; + } + return response; + } else { + retry = 0; + return handleException(ERROR_500, JDBC_CONNECTION_ERROR_MSG + e.getMessage()); + } + } + + private Response buildResponse(Object response) { return Response.ok().entity(response).build(); } - Response buildErrorResponse(Map response) { + private Response buildErrorResponse(Map response) { return Response.status(500).entity(response).build(); } - Map buildErrorResponse(String errorCode, String message) { + private Map buildErrorResponse(String errorCode, String message) { Map error = new HashMap<>(); error.put("errorId", errorCode); error.put("message", message); -- cgit 1.2.3-korg