Commit 0b45150016ad6db5d028777202b7e050683d7dd8
1 parent
393d4c69
Exists in
master
Modificações para funcionar JPATransaction em EAR com mais de um WAR
Showing
1 changed file
with
22 additions
and
8 deletions
Show diff stats
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java
@@ -5,7 +5,6 @@ import java.util.Collections; | @@ -5,7 +5,6 @@ import java.util.Collections; | ||
5 | import java.util.HashMap; | 5 | import java.util.HashMap; |
6 | import java.util.Map; | 6 | import java.util.Map; |
7 | 7 | ||
8 | -import javax.annotation.PostConstruct; | ||
9 | import javax.annotation.PreDestroy; | 8 | import javax.annotation.PreDestroy; |
10 | import javax.enterprise.context.ApplicationScoped; | 9 | import javax.enterprise.context.ApplicationScoped; |
11 | import javax.inject.Inject; | 10 | import javax.inject.Inject; |
@@ -38,23 +37,28 @@ public class EntityManagerFactoryProducer implements Serializable { | @@ -38,23 +37,28 @@ public class EntityManagerFactoryProducer implements Serializable { | ||
38 | @Name("demoiselle-jpa-bundle") | 37 | @Name("demoiselle-jpa-bundle") |
39 | private ResourceBundle bundle; | 38 | private ResourceBundle bundle; |
40 | 39 | ||
41 | - private final Map<String, EntityManagerFactory> cache = Collections | ||
42 | - .synchronizedMap(new HashMap<String, EntityManagerFactory>()); | 40 | + // private final Map<String, EntityManagerFactory> cache = Collections |
41 | + // .synchronizedMap(new HashMap<String, EntityManagerFactory>()); | ||
42 | + | ||
43 | + private final Map<String [], EntityManagerFactory> cache = Collections | ||
44 | + .synchronizedMap(new HashMap<String [], EntityManagerFactory>()); | ||
43 | 45 | ||
44 | public EntityManagerFactory create(String persistenceUnit) { | 46 | public EntityManagerFactory create(String persistenceUnit) { |
45 | EntityManagerFactory factory; | 47 | EntityManagerFactory factory; |
46 | 48 | ||
47 | - if (cache.containsKey(persistenceUnit)) { | ||
48 | - factory = cache.get(persistenceUnit); | 49 | + String[] key = new String [] { persistenceUnit, Thread.currentThread().getContextClassLoader().toString()}; |
50 | + | ||
51 | + if (cache.containsKey(key)) { | ||
52 | + factory = cache.get(key); | ||
49 | } else { | 53 | } else { |
50 | factory = Persistence.createEntityManagerFactory(persistenceUnit); | 54 | factory = Persistence.createEntityManagerFactory(persistenceUnit); |
51 | - cache.put(persistenceUnit, factory); | 55 | + cache.put(key, factory); |
52 | } | 56 | } |
53 | 57 | ||
54 | return factory; | 58 | return factory; |
55 | } | 59 | } |
56 | 60 | ||
57 | - @PostConstruct | 61 | + // @PostConstruct |
58 | public void init() { | 62 | public void init() { |
59 | ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); | 63 | ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); |
60 | 64 | ||
@@ -94,6 +98,16 @@ public class EntityManagerFactoryProducer implements Serializable { | @@ -94,6 +98,16 @@ public class EntityManagerFactoryProducer implements Serializable { | ||
94 | } | 98 | } |
95 | 99 | ||
96 | public Map<String, EntityManagerFactory> getCache() { | 100 | public Map<String, EntityManagerFactory> getCache() { |
97 | - return cache; | 101 | + init(); |
102 | + Map<String, EntityManagerFactory> result = new HashMap<String, EntityManagerFactory>(); | ||
103 | + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | ||
104 | + | ||
105 | + for (String[] key : cache.keySet()) { | ||
106 | + if(key[1].equals(classLoader.toString())) { | ||
107 | + result.put(key[0], cache.get(key)); | ||
108 | + } | ||
109 | + } | ||
110 | + | ||
111 | + return result; | ||
98 | } | 112 | } |
99 | } | 113 | } |