From 6f206af440ee2730edcebde189edc95754733a12 Mon Sep 17 00:00:00 2001 From: cen1 Date: Tue, 4 Aug 2020 17:19:40 +0200 Subject: [PATCH 1/3] added support to ignore framework startup failure on error to establish a db connection --- .../ee/jpa/common/injection/JpaService.java | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/components/jpa/common/src/main/java/com/kumuluz/ee/jpa/common/injection/JpaService.java b/components/jpa/common/src/main/java/com/kumuluz/ee/jpa/common/injection/JpaService.java index b88a5178..1d32306d 100644 --- a/components/jpa/common/src/main/java/com/kumuluz/ee/jpa/common/injection/JpaService.java +++ b/components/jpa/common/src/main/java/com/kumuluz/ee/jpa/common/injection/JpaService.java @@ -20,6 +20,7 @@ */ package com.kumuluz.ee.jpa.common.injection; +import com.kumuluz.ee.configuration.utils.ConfigurationUtil; import com.kumuluz.ee.jpa.common.PersistenceUnitHolder; import com.kumuluz.ee.jpa.common.PersistenceWrapper; import com.kumuluz.ee.jpa.common.exceptions.NoDefaultPersistenceUnit; @@ -32,6 +33,8 @@ import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceContext; import javax.persistence.PersistenceUnit; +import java.util.Optional; +import java.util.logging.Logger; /** * @author Tilen Faganel @@ -40,6 +43,8 @@ @Priority(1) public class JpaService implements JpaInjectionServices { + private Logger log = Logger.getLogger(JpaService.class.getSimpleName());; + @Override public ResourceReferenceFactory registerPersistenceContextInjectionPoint (InjectionPoint injectionPoint) { @@ -59,10 +64,40 @@ public class JpaService implements JpaInjectionServices { } } - PersistenceWrapper wrapper = holder.getEntityManagerFactory(unitName); + //If database is unreachable, by default, framework will fail-early and not initialize due to unhandled exception. + //Error can be ignored with config override per PU, however EntityManager for that connection will be null. + boolean continueOnError = false; + ConfigurationUtil cfg = ConfigurationUtil.getInstance(); + Optional puSizeOpt = cfg.getListSize("kumuluzee.persistence-units"); + + if (puSizeOpt.isPresent()) { + for (int i = 0; i < puSizeOpt.get(); i++) { + Optional puName = cfg.get("kumuluzee.persistence-units[" + i + "].name"); + if (puName.isPresent() && puName.get().equals(unitName)) { + Optional continueOnErrorCfg = cfg.getBoolean("kumuluzee.persistence-units[" + i + "].continue-on-error"); + if (continueOnErrorCfg.isPresent() && continueOnErrorCfg.get()!=null) { + continueOnError = continueOnErrorCfg.get(); + } + break; + } + } + } + + try { + PersistenceWrapper wrapper = holder.getEntityManagerFactory(unitName); - return new PersistenceContextResourceFactory(unitName, wrapper.getEntityManagerFactory(), + return new PersistenceContextResourceFactory(unitName, wrapper.getEntityManagerFactory(), wrapper.getTransactionType(), pc.synchronization()); + } + catch (Exception e) { + if (!continueOnError) { + log.severe("EntityManager for pu "+unitName+" failed to initialize. KumuluzEE initialization failed."); + throw e; + } + } + + log.warning("EntityManager for pu "+unitName+" failed to initialize. KumuluzEE will continue the startup regardless due to config override."); + return null; } @Override From 3c772948a0e8688e3a3e15a19b0895e42459fc21 Mon Sep 17 00:00:00 2001 From: cen1 Date: Fri, 28 Aug 2020 09:25:30 +0200 Subject: [PATCH 2/3] make logger static final --- .../java/com/kumuluz/ee/jpa/common/injection/JpaService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/jpa/common/src/main/java/com/kumuluz/ee/jpa/common/injection/JpaService.java b/components/jpa/common/src/main/java/com/kumuluz/ee/jpa/common/injection/JpaService.java index 1d32306d..db605e3c 100644 --- a/components/jpa/common/src/main/java/com/kumuluz/ee/jpa/common/injection/JpaService.java +++ b/components/jpa/common/src/main/java/com/kumuluz/ee/jpa/common/injection/JpaService.java @@ -43,7 +43,7 @@ @Priority(1) public class JpaService implements JpaInjectionServices { - private Logger log = Logger.getLogger(JpaService.class.getSimpleName());; + private static final Logger log = Logger.getLogger(JpaService.class.getSimpleName()); @Override public ResourceReferenceFactory registerPersistenceContextInjectionPoint From 4eaa952889a80ec4e77a6bd72c3d6a792eb8740a Mon Sep 17 00:00:00 2001 From: cen1 Date: Fri, 28 Aug 2020 09:27:18 +0200 Subject: [PATCH 3/3] capitalize logger name --- .../com/kumuluz/ee/jpa/common/injection/JpaService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/jpa/common/src/main/java/com/kumuluz/ee/jpa/common/injection/JpaService.java b/components/jpa/common/src/main/java/com/kumuluz/ee/jpa/common/injection/JpaService.java index db605e3c..df0b0bf3 100644 --- a/components/jpa/common/src/main/java/com/kumuluz/ee/jpa/common/injection/JpaService.java +++ b/components/jpa/common/src/main/java/com/kumuluz/ee/jpa/common/injection/JpaService.java @@ -43,7 +43,7 @@ @Priority(1) public class JpaService implements JpaInjectionServices { - private static final Logger log = Logger.getLogger(JpaService.class.getSimpleName()); + private static final Logger LOG = Logger.getLogger(JpaService.class.getSimpleName()); @Override public ResourceReferenceFactory registerPersistenceContextInjectionPoint @@ -91,12 +91,12 @@ public class JpaService implements JpaInjectionServices { } catch (Exception e) { if (!continueOnError) { - log.severe("EntityManager for pu "+unitName+" failed to initialize. KumuluzEE initialization failed."); + LOG.severe("EntityManager for pu "+unitName+" failed to initialize. KumuluzEE initialization failed."); throw e; } } - log.warning("EntityManager for pu "+unitName+" failed to initialize. KumuluzEE will continue the startup regardless due to config override."); + LOG.warning("EntityManager for pu "+unitName+" failed to initialize. KumuluzEE will continue the startup regardless due to config override."); return null; }