Joaquín Reñé
yesterday 9430a83dde5d7c3f4535f6c3a5f9e21ac68ac8fa
securis/src/main/java/net/curisit/securis/ioc/EntityManagerProvider.java
....@@ -3,6 +3,7 @@
33 */
44 package net.curisit.securis.ioc;
55
6
+import jakarta.annotation.PostConstruct;
67 import jakarta.enterprise.context.ApplicationScoped;
78 import jakarta.persistence.EntityManager;
89 import jakarta.persistence.EntityManagerFactory;
....@@ -27,15 +28,27 @@
2728 @ApplicationScoped
2829 public class EntityManagerProvider {
2930
30
- @SuppressWarnings("unused")
3131 private static final Logger log = LogManager.getLogger(EntityManagerProvider.class);
3232
3333 /**
3434 * entityManagerFactory<p>
3535 * Application-wide EMF built from persistence.xml PU "localdb".
3636 */
37
- private final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("localdb");
37
+ //private final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("localdb");
38
+ private EntityManagerFactory entityManagerFactory;
3839
40
+ @PostConstruct
41
+ public void init() {
42
+ try {
43
+ log.info("Initializing EntityManagerFactory with persistence unit 'localdb'");
44
+ entityManagerFactory = Persistence.createEntityManagerFactory("localdb");
45
+ log.info("EntityManagerFactory initialized correctly: {}", entityManagerFactory);
46
+ } catch (Exception e) {
47
+ log.error("Error creating EntityManagerFactory for persistence unit 'localdb'", e);
48
+ entityManagerFactory = null;
49
+ }
50
+ }
51
+
3952 /**
4053 * getEntityManager<p>
4154 * Create a new {@link EntityManager}.
....@@ -43,6 +56,17 @@
4356 * @return a new EntityManager; caller must close it
4457 */
4558 public EntityManager getEntityManager() {
46
- return entityManagerFactory.createEntityManager();
59
+ try {
60
+ if (entityManagerFactory == null) {
61
+ log.error("EntityManagerFactory is null");
62
+ return null;
63
+ }
64
+ EntityManager em = entityManagerFactory.createEntityManager();
65
+ log.info("Created EntityManager: {}", em);
66
+ return em;
67
+ } catch (Exception e) {
68
+ log.error("Error creating EntityManager", e);
69
+ return null;
70
+ }
4771 }
4872 }