Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

EXTCDI-305 do qualifier check only for EntityManagers

Other beans can have any Qualifier. 
This commit also fixes the ending of the TransactionContext to allow using
fresh transactions in a @PreDestroy of @TransactionScoped beans.


git-svn-id: https://svn.apache.org/repos/asf/myfaces/extensions/cdi/trunk@1440724 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit 72f34fa055cb43ace0a25001857914831d68e989 1 parent 7645eb2
Mark Struberg authored January 30, 2013
2  jee-modules/jpa-module/api/src/main/java/org/apache/myfaces/extensions/cdi/jpa/api/Transactional.java
@@ -34,8 +34,6 @@
34 34
  * The optional qualifier can be used to specify different entity managers.
35 35
  */
36 36
 
37  
-//TODO instead of using #qualifier it should be possible to use a custom qualifier annotated with @TransactionQualifier
38  
-
39 37
 @InterceptorBinding
40 38
 @Documented
41 39
 @Inherited
49  ...c/main/java/org/apache/myfaces/extensions/cdi/jpa/impl/transaction/TransactionalInterceptorStrategy.java
@@ -136,33 +136,23 @@ public Object execute(InvocationContext invocationContext) throws Exception
136 136
                 for (Map.Entry<String, EntityManager> emsEntry: emsEntries.entrySet())
137 137
                 {
138 138
                     EntityManager em = emsEntry.getValue();
139  
-
140  
-                    try
  139
+                    transaction = em.getTransaction();
  140
+                    if (transaction != null && transaction.isActive())
141 141
                     {
142  
-                        transaction = em.getTransaction();
143  
-
144  
-                        if (transaction != null && transaction.isActive())
  142
+                        try
145 143
                         {
146  
-                            try
147  
-                            {
148  
-                                transaction.rollback();
149  
-                            }
150  
-                            catch (Exception eRollback)
  144
+                            transaction.rollback();
  145
+                        }
  146
+                        catch (Exception eRollback)
  147
+                        {
  148
+                            if(LOGGER.isLoggable(Level.SEVERE))
151 149
                             {
152  
-                                if(LOGGER.isLoggable(Level.SEVERE))
153  
-                                {
154  
-                                    LOGGER.log(Level.SEVERE,
155  
-                                            "Got additional Exception while subsequently " +
156  
-                                                    "rolling back other SQL transactions", eRollback);
157  
-                                }
  150
+                                LOGGER.log(Level.SEVERE,
  151
+                                        "Got additional Exception while subsequently " +
  152
+                                                "rolling back other SQL transactions", eRollback);
158 153
                             }
159 154
                         }
160 155
                     }
161  
-                    catch (IllegalStateException e2)
162  
-                    {
163  
-                        //just happens if the setup is wrong -> we can't do a proper cleanup
164  
-                        //but we have to continue to cleanup the scope
165  
-                    }
166 156
                 }
167 157
 
168 158
                 // drop all EntityManagers from the ThreadLocal
@@ -240,15 +230,18 @@ public Object execute(InvocationContext invocationContext) throws Exception
240 230
                         }
241 231
                     }
242 232
 
  233
+                    ems.remove();
  234
+                    ems.set(null);
  235
+
  236
+                    refCounterMaps.set(null);
  237
+                    refCounterMaps.remove();
  238
+
243 239
                     // and now we close all open transactionscopes and reset the storage
244  
-                    TransactionBeanStorage.getStorage().endAllTransactionScopes();
245  
-                    TransactionBeanStorage.resetStorage();
246  
-                }
247  
-                else
248  
-                {
249  
-                    //just happens if the wrong qualifier is used in an application
250  
-                    TransactionBeanStorage.getStorage().endAllTransactionScopes();
  240
+                    TransactionBeanStorage oldStorage = TransactionBeanStorage.getStorage();
251 241
                     TransactionBeanStorage.resetStorage();
  242
+
  243
+                    // we do this delayed to allow new transactions in a PreDestroy method
  244
+                    oldStorage.endAllTransactionScopes();
252 245
                 }
253 246
             }
254 247
             else
9  ...mpl/src/main/java/org/apache/myfaces/extensions/cdi/jpa/impl/transaction/context/TransactionContext.java
@@ -27,6 +27,7 @@
27 27
 import javax.enterprise.context.spi.Contextual;
28 28
 import javax.enterprise.context.spi.CreationalContext;
29 29
 import javax.enterprise.inject.Typed;
  30
+import javax.persistence.EntityManager;
30 31
 import java.lang.annotation.Annotation;
31 32
 import java.util.Map;
32 33
 
@@ -88,6 +89,14 @@
88 89
 
89 90
     private void checkTransactionBeanEntry(TransactionBeanEntry<?> transactionBeanEntry)
90 91
     {
  92
+        if (! (transactionBeanEntry.getContextualInstance() instanceof EntityManager))
  93
+        {
  94
+            return;
  95
+        }
  96
+
  97
+        // we only do this special check for EntityManagers
  98
+        // this detects the problem when someone uses a @Transactional(SomeQualifier.class)
  99
+        // while using an @Inject @AnotherQualifier EntityManager em; ...
91 100
         String activeTransactionKey = TransactionBeanStorage.getStorage().getActiveTransactionKey();
92 101
 
93 102
         for(Annotation qualifier : transactionBeanEntry.getQualifiers())

0 notes on commit 72f34fa

Please sign in to comment.
Something went wrong with that request. Please try again.