Skip to content

Commit ffc9294

Browse files
committedApr 30, 2013
Clearing hibernate session factories that leak memory on spring refresh:
Investigate possible memory leak in 1.9.0 or one of its bundled modules. - TRUNK-3440
1 parent af6addf commit ffc9294

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
 

‎api/src/main/java/org/openmrs/util/MemoryLeakUtil.java

+22
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
package org.openmrs.util;
1515

1616
import java.lang.reflect.Field;
17+
import java.util.Map;
1718
import java.util.Timer;
1819

1920
import org.apache.commons.logging.Log;
2021
import org.apache.commons.logging.LogFactory;
22+
import org.hibernate.impl.SessionFactoryObjectFactory;
2123

2224
import sun.net.www.http.HttpClient;
2325
import sun.net.www.http.KeepAliveCache;
@@ -81,4 +83,24 @@ public static void shutdownKeepAliveTimer() {
8183
log.error(e.getMessage(), e);
8284
}
8385
}
86+
87+
/**
88+
* Clears the hibernate session factories cached in the SessionFactoryObjectFactory
89+
*/
90+
public static void clearHibernateSessionFactories() {
91+
try {
92+
Field field = SessionFactoryObjectFactory.class.getDeclaredField("INSTANCES");
93+
field.setAccessible(true);
94+
Map instances = (Map) field.get(null);
95+
instances.clear();
96+
97+
field = SessionFactoryObjectFactory.class.getDeclaredField("NAMED_INSTANCES");
98+
field.setAccessible(true);
99+
Map namedInstances = (Map) field.get(null);
100+
namedInstances.clear();
101+
}
102+
catch (Exception ex) {
103+
log.error(ex.getMessage(), ex);
104+
}
105+
}
84106
}

‎api/src/main/java/org/openmrs/util/OpenmrsClassLoader.java

+2
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ public static void destroyInstance() {
323323
log.error(ex.getMessage(), ex);
324324
}
325325
}
326+
327+
MemoryLeakUtil.clearHibernateSessionFactories();
326328

327329
OpenmrsClassScanner.destroyInstance();
328330

0 commit comments

Comments
 (0)
Please sign in to comment.