This is an automated email from the ASF dual-hosted git repository.

boglesby pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 317a706  GEODE-8483: Remove JNDI lookup and check
317a706 is described below

commit 317a7061ae56d97a4d58d740c5af3b022225b36a
Author: Barry Oglesby <[email protected]>
AuthorDate: Thu Sep 10 10:41:55 2020 -1000

    GEODE-8483: Remove JNDI lookup and check
---
 geode-core/build.gradle                            |  7 +++
 .../geode/internal/ra/spi/JCALocalTransaction.java | 63 ++++++++--------------
 .../internal/ra/spi/JCALocalTransactionTest.java   | 63 ++++++++++++++++++++++
 3 files changed, 93 insertions(+), 40 deletions(-)

diff --git a/geode-core/build.gradle b/geode-core/build.gradle
index 551486f..6254ec6 100755
--- a/geode-core/build.gradle
+++ b/geode-core/build.gradle
@@ -155,6 +155,12 @@ configurations {
     jmh {
         extendsFrom testImplementation
     }
+
+    raOutput
+}
+
+artifacts {
+    raOutput raJar
 }
 
 dependencies {
@@ -334,6 +340,7 @@ dependencies {
     testImplementation('org.powermock:powermock-api-mockito2')
     testImplementation('pl.pragmatists:JUnitParams')
     testImplementation('com.tngtech.archunit:archunit-junit4')
+    testImplementation(project(path: ':geode-core', configuration: 'raOutput'))
 
     
testImplementation(files("${System.getProperty('java.home')}/../lib/tools.jar"))
 
diff --git 
a/geode-core/src/jca/java/org/apache/geode/internal/ra/spi/JCALocalTransaction.java
 
b/geode-core/src/jca/java/org/apache/geode/internal/ra/spi/JCALocalTransaction.java
index 5aab51e..a9871d2 100644
--- 
a/geode-core/src/jca/java/org/apache/geode/internal/ra/spi/JCALocalTransaction.java
+++ 
b/geode-core/src/jca/java/org/apache/geode/internal/ra/spi/JCALocalTransaction.java
@@ -17,8 +17,6 @@ package org.apache.geode.internal.ra.spi;
 import javax.resource.ResourceException;
 import javax.resource.spi.LocalTransaction;
 import javax.resource.spi.LocalTransactionException;
-import javax.transaction.SystemException;
-import javax.transaction.TransactionManager;
 
 import org.apache.geode.LogWriter;
 import org.apache.geode.cache.CacheFactory;
@@ -50,43 +48,29 @@ public class JCALocalTransaction implements 
LocalTransaction {
 
   @Override
   public void begin() throws ResourceException {
-    try {
-      if (!this.initDone || this.cache.isClosed()) {
-        this.init();
-      }
-      LogWriter logger = this.cache.getLogger();
-      if (logger.fineEnabled()) {
-        logger.fine("JCALocalTransaction::begin:");
-      }
-      TransactionManager tm = this.cache.getJTATransactionManager();
-      if (this.tid != null) {
-        throw new LocalTransactionException(" A transaction is already in 
progress");
-      }
-      if (tm != null && tm.getTransaction() != null) {
-        if (logger.fineEnabled()) {
-          logger.fine("JCAManagedConnection: JTA transaction is on");
-        }
-        // This is having a JTA transaction. Assuming ignore jta flag is true,
-        // explicitly being a gemfire transaction.
-        TXStateProxy tsp = this.gfTxMgr.getTXState();
-        if (tsp == null) {
-          this.gfTxMgr.begin();
-          tsp = this.gfTxMgr.getTXState();
-          tsp.setJCATransaction();
-          this.tid = tsp.getTransactionId();
-          if (logger.fineEnabled()) {
-            logger.fine("JCALocalTransaction:begun GFE transaction");
-          }
-        } else {
-          throw new LocalTransactionException("GemFire is already associated 
with a transaction");
-        }
-      } else {
-        if (logger.fineEnabled()) {
-          logger.fine("JCAManagedConnection: JTA Transaction does not exist.");
-        }
+    if (!this.initDone || this.cache.isClosed()) {
+      this.init();
+    }
+    if (this.cache.getLogger().fineEnabled()) {
+      this.cache.getLogger().fine("JCALocalTransaction:begin invoked");
+    }
+    if (this.tid != null) {
+      throw new LocalTransactionException(
+          "Transaction with id=" + this.tid + " is already in progress");
+    }
+    TXStateProxy tsp = this.gfTxMgr.getTXState();
+    if (tsp == null) {
+      this.gfTxMgr.begin();
+      tsp = this.gfTxMgr.getTXState();
+      tsp.setJCATransaction();
+      this.tid = tsp.getTransactionId();
+      if (this.cache.getLogger().fineEnabled()) {
+        this.cache.getLogger()
+            .fine("JCALocalTransaction:begin completed transactionId=" + 
this.tid);
       }
-    } catch (SystemException e) {
-      throw new ResourceException(e);
+    } else {
+      throw new LocalTransactionException(
+          "Transaction with state=" + tsp + " is already in progress");
     }
   }
 
@@ -105,8 +89,7 @@ public class JCALocalTransaction implements LocalTransaction 
{
       this.gfTxMgr.commit();
       this.tid = null;
     } catch (Exception e) {
-      // TODO: consider wrapping the cause
-      throw new LocalTransactionException(e.toString());
+      throw new LocalTransactionException(e);
     }
   }
 
diff --git 
a/geode-core/src/test/java/org/apache/geode/internal/ra/spi/JCALocalTransactionTest.java
 
b/geode-core/src/test/java/org/apache/geode/internal/ra/spi/JCALocalTransactionTest.java
new file mode 100644
index 0000000..895eba3
--- /dev/null
+++ 
b/geode-core/src/test/java/org/apache/geode/internal/ra/spi/JCALocalTransactionTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.geode.internal.ra.spi;
+
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import javax.resource.ResourceException;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.geode.internal.cache.GemFireCacheImpl;
+import org.apache.geode.internal.cache.TXManagerImpl;
+import org.apache.geode.internal.cache.TXStateProxy;
+import org.apache.geode.test.fake.Fakes;
+
+public class JCALocalTransactionTest {
+
+  private GemFireCacheImpl cache;
+
+  private TXManagerImpl transactionManager;
+
+  @Before
+  public void setUp() throws Exception {
+    this.cache = Fakes.cache();
+    this.transactionManager = mock(TXManagerImpl.class);
+  }
+
+  @Test
+  public void testCallingBeginDoesBeginTransaction() throws ResourceException {
+    // Create JCALocalTransaction
+    JCALocalTransaction jcaTransaction =
+        new JCALocalTransaction(this.cache, this.transactionManager);
+
+    // Once TXManagerImpl begin is called, add when/thenReturn to return the 
TXStateProxy
+    doAnswer(invocation -> {
+      
when(this.transactionManager.getTXState()).thenReturn(mock(TXStateProxy.class));
+      return null;
+    }).when(this.transactionManager).begin();
+
+    // Begin the transaction
+    jcaTransaction.begin();
+
+    // Verify the TXManagerImpl begins a transaction
+    verify(this.transactionManager, times(1)).begin();
+  }
+}

Reply via email to