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:
parent
4ae1e3075d
commit
bc0199312f
|
|
@ -48,6 +48,22 @@ public abstract class DatabaseManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
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) {
|
if (connection != null) {
|
||||||
try {
|
try {
|
||||||
logger.info("Closing database connection...");
|
logger.info("Closing database connection...");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue