From 894532bfbf6c6a304e90d2ad89f430ce4c50646a Mon Sep 17 00:00:00 2001 From: Ittay Stern Date: Wed, 13 Nov 2019 17:06:13 +0200 Subject: [PATCH] Retry Simulator's DB queries upon failure In tests env, sometimes DB drops unused connections with error: java.sql.SQLNonTransientConnectionException: (conn=3439) Connection reset Issue-ID: VID-647 Change-Id: If8211db653d4b3b3efc4edbe8b3ad6d3dd7d65e2 Signed-off-by: Ittay Stern --- .../onap/simulator/controller/SimulatorController.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/vid-ext-services-simulator/src/main/java/org/onap/simulator/controller/SimulatorController.java b/vid-ext-services-simulator/src/main/java/org/onap/simulator/controller/SimulatorController.java index b6cc6759f..296d9fcbf 100644 --- a/vid-ext-services-simulator/src/main/java/org/onap/simulator/controller/SimulatorController.java +++ b/vid-ext-services-simulator/src/main/java/org/onap/simulator/controller/SimulatorController.java @@ -29,6 +29,7 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Scanner; +import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.PostConstruct; @@ -253,7 +254,7 @@ public class SimulatorController { String userName = reqUri.substring(reqUri.lastIndexOf('/') + 1); TypedQuery userQuery = entityManager.createQuery("select u from fn_user u where u.loginId = :userName", User.class); userQuery.setParameter("userName", userName); - User user = userQuery.getSingleResult(); + User user = doWithSingleRetry(userQuery::getSingleResult); Gson g = new Gson(); String jsonString = g.toJson(user); @@ -269,7 +270,7 @@ public class SimulatorController { return new ResponseEntity<>("Centralized Role Access is disabled", HttpStatus.SERVICE_UNAVAILABLE); } TypedQuery userQuery = entityManager.createQuery("select f from fn_function f", Function.class); - List functions = userQuery.getResultList(); + List functions = doWithSingleRetry(userQuery::getResultList); Gson g = new Gson(); String jsonString = g.toJson(functions); @@ -341,6 +342,16 @@ public class SimulatorController { return responseEntity; } + private T doWithSingleRetry(Supplier supplier) { + try { + return supplier.get(); + } catch (Exception e) { + logger.error("exception was thrown; will retry the same action one more time", e); + // here exceptions will be thrown + return supplier.get(); + } + } + private void register(SimulatorRequestResponseExpectation expectationModel) throws VidSimulatorException{ //Setting request according to what is passed HttpRequest request = HttpRequest.request(); -- 2.16.6