Commit 5bcedfd047b1ba556f894ea7df89a90c98610457

Authored by Ednara Oliveira
1 parent 583e551c
Exists in master

Ajustes no cache de EntityManager

impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java
... ... @@ -40,13 +40,14 @@ public class EntityManagerFactoryProducer implements Serializable {
40 40 // private final Map<String, EntityManagerFactory> cache = Collections
41 41 // .synchronizedMap(new HashMap<String, EntityManagerFactory>());
42 42  
43   - private final Map<String [], EntityManagerFactory> cache = Collections
44   - .synchronizedMap(new HashMap<String [], EntityManagerFactory>());
  43 + private final Map<Key, EntityManagerFactory> cache = Collections
  44 + .synchronizedMap(new HashMap<Key, EntityManagerFactory>());
45 45  
46 46 public EntityManagerFactory create(String persistenceUnit) {
47 47 EntityManagerFactory factory;
48 48  
49   - String[] key = new String [] { persistenceUnit, Thread.currentThread().getContextClassLoader().toString()};
  49 + //String[] key = new String [] { persistenceUnit, Thread.currentThread().getContextClassLoader().toString()};
  50 + Key key = new Key(persistenceUnit, Thread.currentThread().getContextClassLoader());
50 51  
51 52 if (cache.containsKey(key)) {
52 53 factory = cache.get(key);
... ... @@ -102,12 +103,60 @@ public class EntityManagerFactoryProducer implements Serializable {
102 103 Map<String, EntityManagerFactory> result = new HashMap<String, EntityManagerFactory>();
103 104 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
104 105  
105   - for (String[] key : cache.keySet()) {
106   - if(key[1].equals(classLoader.toString())) {
107   - result.put(key[0], cache.get(key));
  106 + for (Key key : cache.keySet()) {
  107 + if(key.classLoader.equals(classLoader)) {
  108 + result.put(key.persistenceUnit, cache.get(key));
108 109 }
109 110 }
110 111  
111 112 return result;
112 113 }
  114 +
  115 +
  116 + class Key{
  117 +
  118 + private String persistenceUnit;
  119 + private ClassLoader classLoader;
  120 +
  121 +
  122 + public Key(String persistenceUnit, ClassLoader classLoader) {
  123 + this.persistenceUnit = persistenceUnit;
  124 + this.classLoader = classLoader;
  125 + }
  126 +
  127 + @Override
  128 + public int hashCode() {
  129 + final int prime = 31;
  130 + int result = 1;
  131 + result = prime * result + ((classLoader == null) ? 0 : classLoader.hashCode());
  132 + result = prime * result + ((persistenceUnit == null) ? 0 : persistenceUnit.hashCode());
  133 + return result;
  134 + }
  135 +
  136 + @Override
  137 + public boolean equals(Object obj) {
  138 + if (this == obj)
  139 + return true;
  140 + if (obj == null)
  141 + return false;
  142 + if (getClass() != obj.getClass())
  143 + return false;
  144 + Key other = (Key) obj;
  145 + if (classLoader == null) {
  146 + if (other.classLoader != null)
  147 + return false;
  148 + } else if (!classLoader.equals(other.classLoader))
  149 + return false;
  150 + if (persistenceUnit == null) {
  151 + if (other.persistenceUnit != null)
  152 + return false;
  153 + } else if (!persistenceUnit.equals(other.persistenceUnit))
  154 + return false;
  155 + return true;
  156 + }
  157 +
  158 + }
  159 +
113 160 }
  161 +
  162 +
... ...