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

jbertram pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git


The following commit(s) were added to refs/heads/main by this push:
     new 8d0fe02a44 ARTEMIS-5562 fix NPE on closeConnectionFactory
8d0fe02a44 is described below

commit 8d0fe02a44144c5505195b3b99d6e8bdf03b2b0b
Author: Marcus Bengtsson <[email protected]>
AuthorDate: Wed Aug 6 16:24:07 2025 +0200

    ARTEMIS-5562 fix NPE on closeConnectionFactory
    
    A NullPointerException was thrown by
    ActiveMQResourceAdapter.closeConnectionFactory() if called multiple
    times with same properties due to factory already been removed from
    knownConnectionFactories.
    
    Note: this was mostly a "cosmetic" issue since exception was caught by
    caller in ActiveMQRAManagedConnection.destroy() and stacktrace logged
    
    Fixed by adding proper null check and a debug log, also adding unit test
    proving issue is solved.
---
 .../activemq/artemis/ra/ActiveMQResourceAdapter.java  | 16 ++++++++++++----
 .../artemis/tests/unit/ra/ResourceAdapterTest.java    | 19 +++++++++++++++++++
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git 
a/artemis-ra/src/main/java/org/apache/activemq/artemis/ra/ActiveMQResourceAdapter.java
 
b/artemis-ra/src/main/java/org/apache/activemq/artemis/ra/ActiveMQResourceAdapter.java
index 0d1ec6b740..cbf45b6b56 100644
--- 
a/artemis-ra/src/main/java/org/apache/activemq/artemis/ra/ActiveMQResourceAdapter.java
+++ 
b/artemis-ra/src/main/java/org/apache/activemq/artemis/ra/ActiveMQResourceAdapter.java
@@ -1490,10 +1490,18 @@ public class ActiveMQResourceAdapter implements 
ResourceAdapter, Serializable {
    }
 
    public synchronized void closeConnectionFactory(ConnectionFactoryProperties 
properties) {
-      Pair<ActiveMQConnectionFactory, AtomicInteger> pair = 
knownConnectionFactories.get(properties);
-      int references = pair.getB().decrementAndGet();
-      if (pair.getA() != null && pair.getA() != 
defaultActiveMQConnectionFactory && references == 0) {
-         knownConnectionFactories.remove(properties).getA().close();
+      Pair<ActiveMQConnectionFactory, AtomicInteger> factoryPair = 
knownConnectionFactories.get(properties);
+      if (factoryPair == null) {
+         logger.debug("No connection factory found for client '{}', probably 
already closed.", properties.getClientID());
+         return;
+      }
+
+      ActiveMQConnectionFactory factory = factoryPair.getA();
+      int referenceCount = factoryPair.getB().decrementAndGet();
+
+      if (factory != null && factory != defaultActiveMQConnectionFactory && 
referenceCount == 0) {
+         knownConnectionFactories.remove(properties);
+         factory.close();
       }
    }
 
diff --git 
a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/ra/ResourceAdapterTest.java
 
b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/ra/ResourceAdapterTest.java
index f45b136ac9..2134b5d912 100644
--- 
a/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/ra/ResourceAdapterTest.java
+++ 
b/tests/unit-tests/src/test/java/org/apache/activemq/artemis/tests/unit/ra/ResourceAdapterTest.java
@@ -655,4 +655,23 @@ public class ResourceAdapterTest extends ActiveMQTestBase {
          server.stop();
       }
    }
+   @Test
+   public void testCloseConnectionFactoryMultipleTimesDoesNotThrow() throws 
Exception {
+      ActiveMQResourceAdapter ra = new ActiveMQResourceAdapter();
+      ra.setConnectorClassName(INVM_CONNECTOR_FACTORY);
+
+      ConnectionFactoryProperties properties = new 
ConnectionFactoryProperties();
+      properties.setClientID("test-client");
+
+      ActiveMQConnectionFactory factory = ra.getConnectionFactory(properties);
+      assertNotNull(factory);
+
+      ra.closeConnectionFactory(properties);
+
+      try {
+         ra.closeConnectionFactory(properties);
+      } catch (NullPointerException e) {
+         fail("NullPointerException was thrown when closing connection factory 
multiple times");
+      }
+   }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact


Reply via email to