Browse Source

Release shared JMS Connection before stop completion callback

Closes gh-30612
pull/30620/head
Juergen Hoeller 3 years ago
parent
commit
06bb6dbcff
  1. 29
      spring-jms/src/main/java/org/springframework/jms/listener/AbstractJmsListeningContainer.java
  2. 12
      spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java

29
spring-jms/src/main/java/org/springframework/jms/listener/AbstractJmsListeningContainer.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -205,10 +205,7 @@ public abstract class AbstractJmsListeningContainer extends JmsDestinationAccess
doInitialize(); doInitialize();
} }
catch (JMSException ex) { catch (JMSException ex) {
synchronized (this.sharedConnectionMonitor) { releaseSharedConnection();
ConnectionFactoryUtils.releaseConnection(this.sharedConnection, getConnectionFactory(), this.autoStartup);
this.sharedConnection = null;
}
throw convertJmsAccessException(ex); throw convertJmsAccessException(ex);
} }
} }
@ -248,10 +245,7 @@ public abstract class AbstractJmsListeningContainer extends JmsDestinationAccess
} }
finally { finally {
if (sharedConnectionEnabled()) { if (sharedConnectionEnabled()) {
synchronized (this.sharedConnectionMonitor) { releaseSharedConnection();
ConnectionFactoryUtils.releaseConnection(this.sharedConnection, getConnectionFactory(), false);
this.sharedConnection = null;
}
} }
} }
} }
@ -391,9 +385,7 @@ public abstract class AbstractJmsListeningContainer extends JmsDestinationAccess
*/ */
protected final void refreshSharedConnection() throws JMSException { protected final void refreshSharedConnection() throws JMSException {
synchronized (this.sharedConnectionMonitor) { synchronized (this.sharedConnectionMonitor) {
ConnectionFactoryUtils.releaseConnection( releaseSharedConnection();
this.sharedConnection, getConnectionFactory(), this.sharedConnectionStarted);
this.sharedConnection = null;
this.sharedConnection = createSharedConnection(); this.sharedConnection = createSharedConnection();
if (this.sharedConnectionStarted) { if (this.sharedConnectionStarted) {
this.sharedConnection.start(); this.sharedConnection.start();
@ -474,6 +466,19 @@ public abstract class AbstractJmsListeningContainer extends JmsDestinationAccess
} }
} }
/**
* Release the shared Connection, if any.
* @since 6.1
* @see ConnectionFactoryUtils#releaseConnection
*/
protected final void releaseSharedConnection() {
synchronized (this.sharedConnectionMonitor) {
ConnectionFactoryUtils.releaseConnection(
this.sharedConnection, getConnectionFactory(), this.sharedConnectionStarted);
this.sharedConnection = null;
}
}
/** /**
* Return the shared JMS Connection maintained by this container. * Return the shared JMS Connection maintained by this container.
* Available after initialization. * Available after initialization.

12
spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java

@ -1245,9 +1245,15 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
private void decreaseActiveInvokerCount() { private void decreaseActiveInvokerCount() {
activeInvokerCount--; activeInvokerCount--;
if (stopCallback != null && activeInvokerCount == 0) { if (activeInvokerCount == 0) {
stopCallback.run(); if (!isRunning()) {
stopCallback = null; // Proactively release shared Connection when stopped.
releaseSharedConnection();
}
if (stopCallback != null) {
stopCallback.run();
stopCallback = null;
}
} }
} }

Loading…
Cancel
Save