Fix half-initialized EMF for JPA Map Storage

Closes #19741
This commit is contained in:
Alexander Schwartz 2023-04-15 19:36:50 +02:00 committed by Hynek Mlnařík
parent 76ee1be76c
commit 8f4c721a7d

View file

@ -339,11 +339,21 @@ public class JpaMapStorageProviderFactory implements
this.validatedModels.clear();
}
volatile boolean initialized;
private void lazyInit() {
if (emf == null) {
/*
On Quarkus, and EMF can be created even when the database is currently not available.
Closing the EMF is not an option, as it is managed by Quarkus.
Therefore, try to initialize it as often as needed, especially for the addSpecificNamedQueries()
which would cause failures later if not initialized here.
*/
if (!initialized) {
synchronized (this) {
if (emf == null) {
this.emf = createEntityManagerFactory();
}
if (!initialized) {
JpaMapUtils.addSpecificNamedQueries(emf);
// consistency check for transaction handling, as this would lead to data-inconsistencies as changes wouldn't commit when expected
@ -353,12 +363,16 @@ public class JpaMapStorageProviderFactory implements
// consistency check for auto-commit, as this would lead to data-inconsistencies as changes wouldn't roll back when expected
EntityManager em = getEntityManager();
em.unwrap(SessionImpl.class).doWork(connection -> {
if (connection.getAutoCommit()) {
throw new ModelException("The database connection must not use auto-commit. For Quarkus, auto-commit was off once JTA was enabled for the EntityManager.");
}
});
em.close();
try {
em.unwrap(SessionImpl.class).doWork(connection -> {
if (connection.getAutoCommit()) {
throw new ModelException("The database connection must not use auto-commit. For Quarkus, auto-commit was off once JTA was enabled for the EntityManager.");
}
});
} finally {
em.close();
}
initialized = true;
}
}
}