[OPENJPA-2834] cache EMF#properties
diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
index 4dd9728..0c8ff9a 100644
--- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
+++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
@@ -75,6 +75,8 @@
private transient StoreCache _cache = null;
private transient QueryResultCache _queryCache = null;
private transient MetamodelImpl _metaModel;
+ private transient Map<String, Object> properties;
+ private transient Map<String, Object> emEmptyPropsProperties;
/**
* Default constructor provided for auto-instantiation.
@@ -111,10 +113,19 @@
@Override
public Map<String,Object> getProperties() {
- Map<String,Object> props = _factory.getProperties();
- // convert to user readable values
- props.putAll(createEntityManager().getProperties());
- return props;
+ if (properties == null) {
+ Map<String,Object> props = _factory.getProperties();
+ // convert to user readable values
+ if (emEmptyPropsProperties != null) {
+ props.putAll(emEmptyPropsProperties);
+ } else {
+ props.putAll(createEntityManager().getProperties());
+ }
+ // no need to sync or volatile, worse case concurrent threads create 2 instances
+ // we just want to avoid to do it after some "init" phase
+ this.properties = props;
+ }
+ return properties;
}
@Override
@@ -201,6 +212,7 @@
props = new HashMap(props);
}
+ boolean canCacheGetProperties = props.isEmpty(); // nominal case
OpenJPAConfiguration conf = getConfiguration();
Log log = conf.getLog(OpenJPAConfiguration.LOG_RUNTIME);
@@ -273,6 +285,13 @@
for (Map.Entry entry : entrySet) {
em.setProperty(entry.getKey().toString(), entry.getValue());
}
+ if (canCacheGetProperties) {
+ if (emEmptyPropsProperties == null) {
+ emEmptyPropsProperties = em.getProperties();
+ } else if (EntityManagerImpl.class.isInstance(em)) {
+ EntityManagerImpl.class.cast(em).setProperties(emEmptyPropsProperties);
+ }
+ }
if (log != null && log.isTraceEnabled()) {
log.trace(this + " created EntityManager " + em + ".");
}
diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
index 35606f0..9782412 100644
--- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
+++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java
@@ -122,6 +122,7 @@
protected RuntimeExceptionTranslator _ret = PersistenceExceptions.getRollbackTranslator(this);
private boolean _convertPositionalParams = false;
private boolean _isJoinedToTransaction;
+ private Map<String, Object> properties;
public EntityManagerImpl() {
// for Externalizable
@@ -1792,6 +1793,10 @@
}
}
+ public void setProperties(final Map<String, Object> emEmptyPropsProperties) {
+ this.properties = emEmptyPropsProperties;
+ }
+
private static class BrokerBytesInputStream extends ObjectInputStream {
private OpenJPAConfiguration conf;
@@ -1935,6 +1940,9 @@
*/
@Override
public Map<String, Object> getProperties() {
+ if (properties != null) {
+ return properties;
+ }
Map<String,Object> props = _broker.getProperties();
for (String s : _broker.getSupportedProperties()) {
String kernelKey = getBeanPropertyName(s);