Liran Zelkha has uploaded a new change for review.

Change subject: core: Cache JNDI lookups and InitialContext creation
......................................................................

core: Cache JNDI lookups and InitialContext creation

This patch caches the lookup for resources and local EJBs in the server.
The goal is to improve performance.

Change-Id: I45ae8d0a90a03c498e19e5429d196924671c073b
Signed-off-by: lzel...@redhat.com <liran.zel...@gmail.com>
---
M 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/EJBUtilsStrategy.java
1 file changed, 28 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/12/16612/1

diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/EJBUtilsStrategy.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/EJBUtilsStrategy.java
index cb766d5..ffa7893 100644
--- 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/EJBUtilsStrategy.java
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/ejb/EJBUtilsStrategy.java
@@ -1,6 +1,7 @@
 package org.ovirt.engine.core.utils.ejb;
 
 import java.util.HashMap;
+import java.util.Map;
 
 import javax.naming.Context;
 import javax.naming.InitialContext;
@@ -27,6 +28,8 @@
     // names
     protected HashMap<BeanType, String> beanTypeToJNDINameMap = new 
HashMap<BeanType, String>();
 
+    static private Map<String, Object> cachedJNDIReferences = new HashMap<>();
+
     protected EJBUtilsStrategy() {
         // Adds JNDI resources,
         addJNDIResources();
@@ -42,6 +45,16 @@
     protected void addJNDIResources() {
         addResourceJNDIName(ContainerManagedResourceType.TRANSACTION_MANAGER, 
"java:jboss/TransactionManager");
         addResourceJNDIName(ContainerManagedResourceType.DATA_SOURCE, 
"java:/ENGINEDataSource");
+    }
+
+    private static InitialContext context;
+
+    private static synchronized InitialContext getInitialContext() throws 
NamingException {
+
+        if (context == null)
+            context = new InitialContext();
+
+        return context;
     }
 
     /**
@@ -61,8 +74,7 @@
         }
 
         try {
-            InitialContext context = new InitialContext();
-            return (T) context.lookup(jndiNameFromMap);
+            return getReference(jndiNameFromMap);
         } catch (NamingException ex) {
             log.error("Error looking up resource " + resourceValue);
             return null;
@@ -95,13 +107,14 @@
             jndiNameSB.append(getBeanSuffix(beanType, proxyType));
 
             if (proxyType == BeanProxyType.LOCAL) {
-                context = new InitialContext();
+//                context = new InitialContext();
+                context = getInitialContext();
             }
 
             // appends "local" or "remote" to the jndi name, depends on the
             // proxy type
             if (context != null) {
-                return (T) context.lookup(jndiNameSB.toString());
+                return getReference(jndiNameSB.toString());
             } else {
                 log.errorFormat("Failed to create InitialContext which is 
currently null," +
                         " possibly because given BeanProxyType is null. Given 
BeanProxyType: {0}",
@@ -118,6 +131,17 @@
         }
     }
 
+    private <T> T getReference(String refName) throws NamingException {
+        if (cachedJNDIReferences.containsKey(refName)) {
+            return (T) cachedJNDIReferences.get(refName);
+        }
+        Object reference = getInitialContext().lookup(refName);
+        cachedJNDIReferences.put(refName, reference);
+
+        return (T) reference;
+
+    }
+
     protected void addResourceJNDIName(ContainerManagedResourceType 
aResourceEnumValue, String aJNDIName) {
         resourceTypeToJNDINameMap.put(aResourceEnumValue, aJNDIName);
     }


-- 
To view, visit http://gerrit.ovirt.org/16612
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I45ae8d0a90a03c498e19e5429d196924671c073b
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Liran Zelkha <liran.zel...@gmail.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to