Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
zvonegit committed Sep 15, 2020
2 parents 561e70d + 00b97c8 commit 56dd477
Showing 1 changed file with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -40,6 +43,8 @@
@Priority(1)
public class JpaService implements JpaInjectionServices {

private static final Logger LOG = Logger.getLogger(JpaService.class.getSimpleName());

@Override
public ResourceReferenceFactory<EntityManager> registerPersistenceContextInjectionPoint
(InjectionPoint injectionPoint) {
Expand All @@ -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<Integer> puSizeOpt = cfg.getListSize("kumuluzee.persistence-units");

if (puSizeOpt.isPresent()) {
for (int i = 0; i < puSizeOpt.get(); i++) {
Optional<String> puName = cfg.get("kumuluzee.persistence-units[" + i + "].name");
if (puName.isPresent() && puName.get().equals(unitName)) {
Optional<Boolean> 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
Expand Down

0 comments on commit 56dd477

Please sign in to comment.