Thread safe entity manager factory
@@ -45,7 +45,10 @@ public class AppCatalogJPAUtils {
@PersistenceContext(unitName = "appcatalog_data")
private static EntityManager appCatEntityManager;
+ private static Object lock = new Object();
+
public static EntityManager getEntityManager() throws ApplicationSettingsException {
+ synchronized (lock) {
if (factory == null) {
String connectionProperties = "DriverClassName=" + readServerProperties(APPCATALOG_JDBC_DRIVER) + "," +
"Url=" + readServerProperties(APPCATALOG_JDBC_URL) + "?autoReconnect=true," +
@@ -58,6 +61,7 @@ public class AppCatalogJPAUtils {
properties.put("openjpa.ConnectionProperties", connectionProperties);
properties.put("openjpa.DynamicEnhancementAgent", "true");
properties.put("openjpa.RuntimeUnenhancedClasses", "unsupported");
+ //properties.put("openjpa.Multithreaded", "true");
// For app catalog, we don't need caching
// properties.put("openjpa.DataCache","" + readServerProperties(JPA_CACHE_ENABLED) + "(CacheSize=" + Integer.valueOf(readServerProperties(JPA_CACHE_SIZE)) + ", SoftReferenceSize=0)");
// properties.put("openjpa.QueryCache","" + readServerProperties(JPA_CACHE_ENABLED) + "(CacheSize=" + Integer.valueOf(readServerProperties(JPA_CACHE_SIZE)) + ", SoftReferenceSize=0)");
@@ -77,6 +81,7 @@ public class AppCatalogJPAUtils {
if (appCatEntityManager != null) {
appCatEntityManager.clear();
}
+ }
return appCatEntityManager;
}
