18 package org.turro.jpa;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.persistence.EntityManager;
26 import javax.persistence.EntityManagerFactory;
27 import javax.persistence.Persistence;
28 import javax.persistence.PersistenceUnit;
29 import javax.persistence.Query;
30 import org.amic.util.file.FileSelector;
31 import org.amic.util.file.FileUtil;
32 import org.turro.string.Strings;
33 import org.turro.action.IElephantCloseable;
34 import org.turro.annotation.ElephantCloseable;
35 import org.turro.elephant.context.ElephantContext;
36 import org.turro.jpa.config.JpaConfig;
45 private static final HashMap<String, EntityManager> cachedEMs=
new HashMap<>();
46 private static final HashMap<String, Long> lastAccessEMs=
new HashMap<>();
47 private static final long CLEAR_CACHE = 2000L, CLOSE_CACHE = 360000L;
50 private EntityManagerFactory emf =
null;
52 private String pu, conf;
55 EntityManagerFactory factory = getFactory(pu, conf);
57 return factory.createEntityManager();
65 EntityManager cachedEM = cachedEMs.get(pu);
66 Long lastAccess = lastAccessEMs.get(pu);
67 Long time =
new Date().getTime();
68 if(lastAccess ==
null) lastAccess = time;
69 EntityManagerFactory factory = getFactory(pu, conf);
71 if(cachedEM ==
null || !(cachedEM.isOpen())) {
72 cachedEM = factory.createEntityManager();
73 }
else if(time > lastAccess + CLEAR_CACHE) {
75 }
else if(time > lastAccess + CLOSE_CACHE) {
79 cachedEM = factory.createEntityManager();
82 lastAccessEMs.put(pu, time);
83 cachedEMs.put(pu, cachedEM);
89 public EntityManagerFactory
getEmf() {
93 private synchronized EntityManagerFactory getFactory(String pu, String conf) {
95 doCreateFactory(pu, conf);
100 private void doCreateFactory(String pu1, String conf1) {
107 emf = Persistence.createEntityManagerFactory(this.pu, JpaConfig.from(pu1, conf1));
108 FileSelector fs =
new FileSelector(ElephantContext.getRealPath(
"/WEB-INF/_autoupdate"), conf1 +
"\\_.*\\.sql");
109 if(fs.getFiles() !=
null && fs.getFiles().length > 0) {
110 EntityManager em = emf.createEntityManager();
112 for(File f : fs.getFiles()) {
113 em.getTransaction().begin();
116 for(String sql : FileUtil.getContent(f,
"UTF-8",
";",
new String[] {
"--",
"/*"})) {
117 if(!Strings.isBlank(sql)) {
119 String execSql = sql.substring(0, sql.length() - 1);
122 Query q = em.createNativeQuery(execSql.replaceAll(
"\\\\'",
"''"));
124 }
catch(Exception ex) {
125 Logger.getLogger(DaoFactory.class.getName()).log(Level.FINEST, ElephantContext.logMsg(
"AutoUpdate:" + f.getName() +
":" + sql), ex);
129 em.getTransaction().commit();
130 }
catch (Exception ex) {
131 if(em.getTransaction().isActive()) {
132 em.getTransaction().rollback();
134 Logger.getLogger(DaoFactory.class.getName()).log(Level.FINEST, ElephantContext.logMsg(
"AutoUpdate:" + f.getName()), ex);
136 FileUtil.deleteFile(f);
146 if(emf !=
null) emf.close();
152 cachedEMs.values().forEach((em) -> {
EntityManagerFactory getEmf()
EntityManager createEntityManager(String pu, String conf)
synchronized EntityManager createCachedEntityManager(String pu, String conf)