You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/07/04 13:27:27 UTC

[commons-dbcp] branch master updated: Reuse pattern of throwing XAException instead of NullPointerException in LocalXAConnectionFactory.LocalXAResource

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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git


The following commit(s) were added to refs/heads/master by this push:
     new b806eded Reuse pattern of throwing XAException instead of NullPointerException in LocalXAConnectionFactory.LocalXAResource
b806eded is described below

commit b806eded4ab1446c70e6b012d394ee3d9fe9902b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jul 4 09:27:21 2022 -0400

    Reuse pattern of throwing XAException instead of NullPointerException in
    LocalXAConnectionFactory.LocalXAResource
---
 src/changes/changes.xml                                  |  3 +++
 .../commons/dbcp2/managed/LocalXAConnectionFactory.java  | 16 ++++++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 3e2603ea..847d900a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -101,6 +101,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="fix" due-to="SpotBugs, Gary Gregory">
         Performance: Refactor to use a static inner class in DataSourceXAConnectionFactory.
       </action>
+      <action dev="ggregory" type="fix" due-to="SpotBugs, Gary Gregory">
+        Reuse pattern of throwing XAException instead of NullPointerException in LocalXAConnectionFactory.LocalXAResource.
+      </action>
       <!-- ADD -->
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add and use AbandonedTrace#setLastUsed(Instant).
diff --git a/src/main/java/org/apache/commons/dbcp2/managed/LocalXAConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/managed/LocalXAConnectionFactory.java
index ab495f72..8e7aa6fc 100644
--- a/src/main/java/org/apache/commons/dbcp2/managed/LocalXAConnectionFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/managed/LocalXAConnectionFactory.java
@@ -67,6 +67,13 @@ public class LocalXAConnectionFactory implements XAConnectionFactory {
             this.connection = localTransaction;
         }
 
+        private Xid checkCurrentXid() throws XAException {
+            if (this.currentXid == null) {
+                throw new XAException("There is no current transaction");
+            }
+            return currentXid;
+        }
+
         /**
          * Commits the transaction and restores the original auto commit setting.
          *
@@ -80,10 +87,7 @@ public class LocalXAConnectionFactory implements XAConnectionFactory {
         @Override
         public synchronized void commit(final Xid xid, final boolean flag) throws XAException {
             Objects.requireNonNull(xid, "xid");
-            if (this.currentXid == null) {
-                throw new XAException("There is no current transaction");
-            }
-            if (!this.currentXid.equals(xid)) {
+            if (!checkCurrentXid().equals(xid)) {
                 throw new XAException("Invalid Xid: expected " + this.currentXid + ", but was " + xid);
             }
 
@@ -122,7 +126,7 @@ public class LocalXAConnectionFactory implements XAConnectionFactory {
         @Override
         public synchronized void end(final Xid xid, final int flag) throws XAException {
             Objects.requireNonNull(xid, "xid");
-            if (!this.currentXid.equals(xid)) {
+            if (!checkCurrentXid().equals(xid)) {
                 throw new XAException("Invalid Xid: expected " + this.currentXid + ", but was " + xid);
             }
 
@@ -230,7 +234,7 @@ public class LocalXAConnectionFactory implements XAConnectionFactory {
         @Override
         public synchronized void rollback(final Xid xid) throws XAException {
             Objects.requireNonNull(xid, "xid");
-            if (!this.currentXid.equals(xid)) {
+            if (!checkCurrentXid().equals(xid)) {
                 throw new XAException("Invalid Xid: expected " + this.currentXid + ", but was " + xid);
             }