deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tandrasc...@apache.org
Subject deltaspike git commit: DELTASPIKE-1161 [perf] avoid Instance#Select
Date Fri, 03 Jun 2016 08:33:52 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master d2adacf41 -> 96761cdbf


DELTASPIKE-1161 [perf] avoid Instance#Select

Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/96761cdb
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/96761cdb
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/96761cdb

Branch: refs/heads/master
Commit: 96761cdbf78f1ed63921aff78a007c8625dbc697
Parents: d2adacf
Author: tandraschko <tandraschko@apache.org>
Authored: Fri Jun 3 10:33:05 2016 +0200
Committer: tandraschko <tandraschko@apache.org>
Committed: Fri Jun 3 10:33:05 2016 +0200

----------------------------------------------------------------------
 .../data/impl/handler/EntityManagerRef.java     | 24 +++++++-
 .../impl/handler/EntityManagerRefLookup.java    | 64 ++++++++++++++++++--
 2 files changed, 81 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/96761cdb/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRef.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRef.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRef.java
index 8e6dad9..5039003 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRef.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRef.java
@@ -24,13 +24,20 @@ import org.apache.deltaspike.data.api.EntityManagerResolver;
 
 public class EntityManagerRef
 {
-    private Class<? extends EntityManagerResolver> entityManagerResolverClass;
-    private DependentProvider<? extends EntityManagerResolver> entityManagerResolverDependentProvider;
     private EntityManager entityManager;
+    private DependentProvider<? extends EntityManager> entityManagerDependentProvider;
+    
+    private Class<? extends EntityManagerResolver> entityManagerResolverClass;
     private EntityManagerResolver entityManagerResolver;
-
+    private DependentProvider<? extends EntityManagerResolver> entityManagerResolverDependentProvider;
+        
     public void release()
     {
+        if (entityManagerDependentProvider != null)
+        {
+            entityManagerDependentProvider.destroy();
+        }
+        
         if (entityManagerResolverDependentProvider != null)
         {
             entityManagerResolverDependentProvider.destroy();
@@ -77,4 +84,15 @@ public class EntityManagerRef
     {
         this.entityManagerResolver = entityManagerResolver;
     }
+
+    public DependentProvider<? extends EntityManager> getEntityManagerDependentProvider()
+    {
+        return entityManagerDependentProvider;
+    }
+
+    public void setEntityManagerDependentProvider(
+            DependentProvider<? extends EntityManager> entityManagerDependentProvider)
+    {
+        this.entityManagerDependentProvider = entityManagerDependentProvider;
+    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/96761cdb/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRefLookup.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRefLookup.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRefLookup.java
index aa3ea3b..c43b42f 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRefLookup.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerRefLookup.java
@@ -18,9 +18,14 @@
  */
 package org.apache.deltaspike.data.impl.handler;
 
+import java.lang.annotation.Annotation;
+import java.util.Set;
 import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
 import javax.inject.Inject;
 import javax.persistence.EntityManager;
+import org.apache.deltaspike.core.api.provider.BeanManagerProvider;
 
 import org.apache.deltaspike.core.api.provider.BeanProvider;
 import org.apache.deltaspike.data.impl.meta.RepositoryComponent;
@@ -30,11 +35,51 @@ import org.apache.deltaspike.jpa.spi.entitymanager.ActiveEntityManagerHolder;
 public class EntityManagerRefLookup
 {
     @Inject
-    private EntityManager entityManager;
-
-    @Inject
     private ActiveEntityManagerHolder activeEntityManagerHolder;
 
+    private volatile Boolean globalEntityManagerInitialized;
+    private boolean globalEntityManagerIsNormalScope;
+    private EntityManager globalEntityManager;
+    
+    private void lazyInitGlobalEntityManager()
+    {
+        if (this.globalEntityManagerInitialized == null)
+        {
+            initGlobalEntityManager();
+        }
+    }
+
+    private synchronized void initGlobalEntityManager()
+    {
+        // switch into paranoia mode
+        if (this.globalEntityManagerInitialized == null)
+        {
+            this.globalEntityManagerInitialized = true;
+            
+            BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
+            Set<Bean<?>> beans = beanManager.getBeans(EntityManager.class);
+
+            if (!beans.isEmpty() && beans.size() == 1)
+            {
+                Class<? extends Annotation> scope = beanManager.resolve(beans).getScope();
+                globalEntityManagerIsNormalScope = beanManager.isNormalScope(scope);
+
+                if (globalEntityManagerIsNormalScope)
+                {
+                    Bean<?> bean = beans.iterator().next();
+                    globalEntityManager = (EntityManager) beanManager.getReference(bean,
+                            EntityManager.class,
+                            beanManager.createCreationalContext(bean));
+                }
+            }
+            else
+            {
+                throw new IllegalArgumentException(
+                        "None or multiple EntityManager's found with the default qualifier.");
+            }            
+        }
+    }
+    
     public EntityManagerRef lookupReference(final RepositoryComponent repository)
     {
         EntityManagerRef ref = new EntityManagerRef();
@@ -73,7 +118,18 @@ public class EntityManagerRefLookup
             }
             else
             {
-                ref.setEntityManager(entityManager);
+                lazyInitGlobalEntityManager();
+                if (globalEntityManagerIsNormalScope)
+                {
+                    ref.setEntityManager(globalEntityManager);
+                }
+                else
+                {
+                    ref.setEntityManagerDependentProvider(
+                            BeanProvider.getDependent(EntityManager.class));
+                    ref.setEntityManager(
+                            ref.getEntityManagerDependentProvider().get());
+                }
             }
         }
 


Mime
View raw message