deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject deltaspike git commit: DELTASPIKE-917 separated lookup
Date Thu, 04 Jun 2015 17:46:19 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 7545ab8e9 -> cd0bf79d3


DELTASPIKE-917 separated lookup

 (required by EnvironmentAwareTransactionStrategy for unmanaged threads)


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

Branch: refs/heads/master
Commit: cd0bf79d3109b15719372eea832f0c6823cfc14a
Parents: 7545ab8
Author: gpetracek <gpetracek@apache.org>
Authored: Thu Jun 4 19:43:13 2015 +0200
Committer: gpetracek <gpetracek@apache.org>
Committed: Thu Jun 4 19:45:43 2015 +0200

----------------------------------------------------------------------
 .../BeanManagedUserTransactionStrategy.java     | 26 ++++-----
 .../transaction/UserTransactionResolver.java    | 57 ++++++++++++++++++++
 2 files changed, 71 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cd0bf79d/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/BeanManagedUserTransactionStrategy.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/BeanManagedUserTransactionStrategy.java
b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/BeanManagedUserTransactionStrategy.java
index 35e0ad3..e5af662 100644
--- a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/BeanManagedUserTransactionStrategy.java
+++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/BeanManagedUserTransactionStrategy.java
@@ -19,14 +19,16 @@
 package org.apache.deltaspike.jpa.impl.transaction;
 
 import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.core.api.provider.DependentProvider;
 import org.apache.deltaspike.core.impl.util.JndiUtils;
 import org.apache.deltaspike.core.util.ExceptionUtils;
 import org.apache.deltaspike.jpa.api.transaction.TransactionConfig;
 import org.apache.deltaspike.jpa.impl.transaction.context.EntityManagerEntry;
 
-import javax.annotation.Resource;
 import javax.enterprise.context.Dependent;
 import javax.enterprise.inject.Alternative;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
 import javax.interceptor.InvocationContext;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityTransaction;
@@ -50,17 +52,16 @@ import java.util.logging.Logger;
 //TODO move to a separated ds-jta module and use @Specializes -> no additional config
is needed
 public class BeanManagedUserTransactionStrategy extends ResourceLocalTransactionStrategy
 {
-    protected static final String USER_TRANSACTION_JNDI_NAME = "java:comp/UserTransaction";
     protected static final String TRANSACTION_SYNC_REGISTRY_JNDI_NAME = "java:comp/TransactionSynchronizationRegistry";
 
     private static final long serialVersionUID = -2432802805095533499L;
 
     private static final Logger LOGGER = Logger.getLogger(BeanManagedUserTransactionStrategy.class.getName());
 
-    private transient TransactionConfig transactionConfig;
+    @Inject
+    private BeanManager beanManager;
 
-    @Resource
-    private UserTransaction userTransaction;
+    private transient TransactionConfig transactionConfig;
 
     @Override
     protected EntityManagerEntry createEntityManagerEntry(
@@ -171,18 +172,19 @@ public class BeanManagedUserTransactionStrategy extends ResourceLocalTransaction
 
     protected UserTransaction resolveUserTransaction()
     {
-        if (userTransaction != null)
-        {
-            return userTransaction;
-        }
-
+        //manual lookup needed because injecting UserTransactionResolver can fail (see the
comment there)
         try
         {
-            return JndiUtils.lookup(USER_TRANSACTION_JNDI_NAME, UserTransaction.class);
+            DependentProvider<UserTransactionResolver> provider =
+                BeanProvider.getDependent(this.beanManager, UserTransactionResolver.class);
+
+            UserTransaction userTransaction = provider.get().resolveUserTransaction();
+
+            provider.destroy();
+            return userTransaction;
         }
         catch (Exception e)
         {
-            // do nothing it was just a try
             return null;
         }
     }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cd0bf79d/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/UserTransactionResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/UserTransactionResolver.java
b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/UserTransactionResolver.java
new file mode 100644
index 0000000..92a734a
--- /dev/null
+++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/UserTransactionResolver.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.jpa.impl.transaction;
+
+import org.apache.deltaspike.core.impl.util.JndiUtils;
+
+import javax.annotation.Resource;
+import javax.enterprise.context.Dependent;
+import javax.transaction.UserTransaction;
+import java.io.Serializable;
+
+//the separated logic allows a lazy lookup of this bean and
+//avoids that the injection of UserTransaction fails in an unmanaged thread (see DELTASPIKE-917).
+@Dependent
+public class UserTransactionResolver implements Serializable
+{
+    protected static final String USER_TRANSACTION_JNDI_NAME = "java:comp/UserTransaction";
+
+    private static final long serialVersionUID = -1432802805095533499L;
+
+    @Resource
+    private UserTransaction userTransaction;
+
+    public UserTransaction resolveUserTransaction()
+    {
+        if (userTransaction != null)
+        {
+            return userTransaction;
+        }
+
+        try
+        {
+            return JndiUtils.lookup(USER_TRANSACTION_JNDI_NAME, UserTransaction.class);
+        }
+        catch (Exception e)
+        {
+            // do nothing it was just a try
+            return null;
+        }
+    }
+}


Mime
View raw message