Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 3.0.3
* `EntityManagerController`
* Make it possible to configure `closeEntityManagerOnCleanupWithoutCheck`

# 3.0.2
* Fix doubled values for `ContainerMemory`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,40 @@ public class EntityManagerController implements AutoCloseable
protected final List<EntityManager> 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?
* <p>
* Setting this to {@code true} improves performance.
* </p>
* <p>
* {@code true} by default as Entity Managers are assumed to be container(=framework) managed <br/>and not
* application(=developer) managed and "randomly" closed during operations.
* </p>
* <p>
* Setting this option to {@code false} usually indicates major design flaws in the corresponding code.
* </p>
*/
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.
*
* <p>
* It may be a good idea to close the EntityManager, when you're finished with it.
* </p>
* <p>
* All created EntityManager are automatically cleaned up once {@link #close()} is called.
* </p>
*
Expand All @@ -77,7 +98,11 @@ public void close()
{
em.getTransaction().rollback();
}
em.close();

if(this.closeEntityManagerOnCleanupWithoutCheck || !em.isOpen())
{
em.close();
}
}
catch(final Exception e)
{
Expand Down