Add null and open-state checks for EntityManager and EntityManagerFactory closure

- Ensure safe closure of `EntityManager` and `EntityManagerFactory`.
- Add logging for successful closure and error handling during exceptions.
This commit is contained in:
Gustavo Henrique Santos Souza de Miranda 2025-11-27 18:55:43 -03:00
parent 4ae1e3075d
commit bc0199312f
1 changed files with 16 additions and 0 deletions

View File

@ -48,6 +48,22 @@ public abstract class DatabaseManager {
}
public void close() {
if (entityManager != null && entityManager.isOpen()) {
try {
entityManager.close();
logger.info("EntityManager closed");
} catch (Exception e) {
logger.error("Error closing EntityManager: {}", e.getMessage());
}
}
if (entityManagerFactory != null && entityManagerFactory.isOpen()) {
try {
entityManagerFactory.close();
logger.info("EntityManagerFactory closed");
} catch (Exception e) {
logger.error("Error closing EntityManagerFactory: {}", e.getMessage());
}
}
if (connection != null) {
try {
logger.info("Closing database connection...");