diff --git a/CHANGELOG.md b/CHANGELOG.md index e1192110..5431d761 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 3.0.3 +* `EntityManagerController` + * Make it possible to configure `closeEntityManagerOnCleanupWithoutCheck` + # 3.0.2 * Fix doubled values for `ContainerMemory` diff --git a/db-jdbc/src/main/java/software/xdev/tci/db/persistence/EntityManagerController.java b/db-jdbc/src/main/java/software/xdev/tci/db/persistence/EntityManagerController.java index 58ded1c9..ffc1e557 100644 --- a/db-jdbc/src/main/java/software/xdev/tci/db/persistence/EntityManagerController.java +++ b/db-jdbc/src/main/java/software/xdev/tci/db/persistence/EntityManagerController.java @@ -39,19 +39,40 @@ public class EntityManagerController implements AutoCloseable protected final List activeEms = Collections.synchronizedList(new ArrayList<>()); protected final EntityManagerFactory emf; + protected boolean closeEntityManagerOnCleanupWithoutCheck = true; + public EntityManagerController(final EntityManagerFactory emf) { this.emf = Objects.requireNonNull(emf); } + /** + * Should it be assumed that each {@link EntityManager} was NOT closed and the {@link EntityManager#isOpen()} + * can be + * skipped? + *

+ * Setting this to {@code true} improves performance. + *

+ *

+ * {@code true} by default as Entity Managers are assumed to be container(=framework) managed
and not + * application(=developer) managed and "randomly" closed during operations. + *

+ *

+ * Setting this option to {@code false} usually indicates major design flaws in the corresponding code. + *

+ */ + public EntityManagerController closeEntityManagerOnCleanupWithoutCheck( + final boolean closeEntityManagerOnCleanupWithoutCheck) + { + this.closeEntityManagerOnCleanupWithoutCheck = closeEntityManagerOnCleanupWithoutCheck; + return this; + } + /** * Creates a new {@link EntityManager} with an internal {@link EntityManagerFactory}, which can be used to load and * save data in the database. * *

- * It may be a good idea to close the EntityManager, when you're finished with it. - *

- *

* All created EntityManager are automatically cleaned up once {@link #close()} is called. *

* @@ -77,7 +98,11 @@ public void close() { em.getTransaction().rollback(); } - em.close(); + + if(this.closeEntityManagerOnCleanupWithoutCheck || !em.isOpen()) + { + em.close(); + } } catch(final Exception e) {