# JmsConsumer does not close connection on stream restart

**URL:** <https://discuss.akka.io/t/jmsconsumer-does-not-close-connection-on-stream-restart/10190>\
**Category:** Akka Streams & Alpakka\
**Created:** [February 22, 2023, 3:06pm UTC](https://discuss.akka.io/t/jmsconsumer-does-not-close-connection-on-stream-restart/10190 "2023-02-22T15:06:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruno](https://avatars.discourse-cdn.com/v4/letter/b/c77e96/32.png) [@Bruno](https://discuss.akka.io/u/Bruno)\
**Post date:** [February 22, 2023, 3:06pm UTC](https://discuss.akka.io/t/jmsconsumer-does-not-close-connection-on-stream-restart/10190/1 "2023-02-22T15:06:57Z")

</div>

Greetings all,

I came across some struggle using akka-stream-alpakka-jms\_2.13-5.0.0.

**The problem**

given a ConnectionFactory set with a clientId (due to being a durable topic subscription solution)

```
var connectionFactory = new ActiveMQConnectionFactory()
connectionFactory.setBrokerURL("vm://localhost:8000")
connectionFactory.setClientID("some-client-id")

```

and a JmsConsumerSettings

```
  JmsConsumerSettings(system, connectionFactory)
    .withDurableTopic("test-topic", "some-subscriber")
    .withSessionCount(1)
    .withAckTimeout(3.seconds)
    .withFailStreamOnAckTimeout(true)

```

when ack timeout is reached, the exception is thrown and the stream begins to restart

```
  [WARN] [FxTradeBridgeTest-akka.actor.default-dispatcher-26] [RestartWithBackoffSource(akka://FxTradeBridgeTest)] Restarting stream due to failure [1]: akka.stream.alpakka.jms.JmsTxAckTimeout: The TxEnvelope didn't get committed or rolled back within ack-timeout (3 seconds)

```

when RestartSource kicks off and a new connection is attempted, it throws InvalidClientIDException, for it cannot have multiple connections with the same clientId

```
 javax.jms.InvalidClientIDException: Broker: localhost - Client: some-client-id already connected from vm://localhost#4

```

**Analysis**

on stream failure

> when JmsConnector\>finishStop\>closeSessions() gets called
> 
> - the session is closed
> - the connection remains open  
> ![image](https://us1.discourse-cdn.com/flex020/uploads/discuss_lightbend/original/2X/2/25c75d90dc7f2b51ad02c26eabb39818704fb190.png)

> PS: if at this moment I invoke connection.close() via evaluator, the stream is restarted gracefully and a new connection is successfully created!

Could you please advise / share a workaround?

Thanks a bunch!

---

<div class="post-metadata">

**Author:** ![ennru](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/ennru/32/1495_2.png) [@ennru](https://discuss.akka.io/u/ennru)\
**Post date:** [March 29, 2023, 1:33pm UTC](https://discuss.akka.io/t/jmsconsumer-does-not-close-connection-on-stream-restart/10190/2 "2023-03-29T13:33:17Z")

</div>

Hi Bruno,

Thank you for reporting.  
Your analysis seems correct, the connection does not get shut down (in time) before the new connection attempt is made.

In the `JmsTxSourceStage.onSessionOpened` the timeout triggers a `rollback()` on the JMS session, but it possibly should turn down the connection as the stage is about to fail when `FailStreamOnAckTimeout` is enabled.

I can’t think of a workaround. It would be great to show this problem in an integration test.

Cheers,  
Enno.
