# Double closing a Transaction in a try-with-resource?

**URL:** <https://discuss.elastic.co/t/double-closing-a-transaction-in-a-try-with-resource/264920>\
**Category:** APM\
**Tags:** java\
**Created:** [February 19, 2021, 10:52pm UTC](https://discuss.elastic.co/t/double-closing-a-transaction-in-a-try-with-resource/264920 "2021-02-19T22:52:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![b333to](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/b333to/32/78726_2.png) [@b333to](https://discuss.elastic.co/u/b333to)\
**Post date:** [February 19, 2021, 10:52pm UTC](https://discuss.elastic.co/t/double-closing-a-transaction-in-a-try-with-resource/264920/1 "2021-02-19T22:52:36Z")

</div>

Hello I have the following `WARN` log:

`WARN co.elastic.apm.agent.impl.transaction.AbstractSpan - End has already been called:...`

And it's coming from this block of code:

```auto
final Transaction transaction = ElasticApm.startTransactionWithRemoteParent(httpHeaders);

try (final Scope scope = transaction.activate()) {
 // fun stuff here ...
  return Response.ok(Object);
} catch (final Exception e) {
  transaction.captureException(e);
  throw e;
} finally {
  transaction.end()
}

```

I removed the `finally` part of the code and the `WARN` log doesn't appear anymore. From what I understand, `close()` is called on the `Scope` object when the `try` block is done. And from the Elastic documentation, regarding the `Transaction.activate()`, method:

_Makes this transaction the active transaction on the current thread until [`Scope.close()`](https://javadoc.io/static/co.elastic.apm/apm-agent-api/1.4.0/co/elastic/apm/api/Scope.html#close--) has been called._

Now my question is. Does declaring the `Transaction` object as `final` and then calling the `.activate()` method on it, qualifies it as a resource to be automatically closed by the _try-with-resource_ code block? If this is not the case, then does is the `Transaction` ended when `close()` is called on `Scope` Object?

- Using Java SDK 11

Thanks!

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [February 22, 2021, 5:47am UTC](https://discuss.elastic.co/t/double-closing-a-transaction-in-a-try-with-resource/264920/2 "2021-02-22T05:47:57Z")

</div>

Hi and welcome to our forum 🎉

> [@b333to](#):
>
> Now my question is. Does declaring the `Transaction` object as `final` and then calling the `.activate()` method on it, qualifies it as a resource to be automatically closed by the _try-with-resource_ code block? If this is not the case, then does is the `Transaction` ended when `close()` is called on `Scope` Object?

Declaring the `transaction` local variable as `final` means it cannot point to anything else after being initialized, but it doesn't say anything about the internal state of the Object it points to.  
More importantly, the `AutoCloseable` resource in this try block is the `Scope` object, which is closed when the block is exited, and which, when being closed, **does not end the underlying transaction**. The Scope's closing only means that the related transaction is deactivated on the thread. You can potentially activate and close scopes of the same transaction on multiple threads or multiple times at the same thread.

So, removing the `finally` block is not the right way to fix your issue. It should be restored here and you need to find where else this transaction is being ended. Note that since you activate the transaction here, it is accessible across everything that happens on that thread during your `fun stuff` execution, so anything can get it and end it.  
Maybe you are creating a transaction where it is not needed? What framework are you using here? Isn't it covered by our [builtin instrumentation](https://www.elastic.co/guide/en/apm/agent/java/current/supported-technologies-details.html#supported-web-frameworks)?

---

<div class="post-metadata">

**Author:** ![b333to](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/b333to/32/78726_2.png) [@b333to](https://discuss.elastic.co/u/b333to)\
**Post date:** [February 22, 2021, 4:57pm UTC](https://discuss.elastic.co/t/double-closing-a-transaction-in-a-try-with-resource/264920/3 "2021-02-22T16:57:07Z")

</div>

Hey, thanks for your reply!

> [@Eyal\_Koren](#):
>
> Maybe you are creating a transaction where it is not needed? What framework are you using here? Isn't it covered by our [builtin instrumentation](https://www.elastic.co/guide/en/apm/agent/java/current/supported-technologies-details.html#supported-web-frameworks)?

- I believe this might be a race condition? That same block of code appears multiple times in my class; one for each call: GET, PUT, DELETE. Maybe one call closes the transaction and then when the other tries to close it, that's when the `WARN` log is thrown?
- The framework I am using here is `JAX-RS`.
- I am not 100% sure of what the question `Isn't it covered by our builtin instrumentation?` is referring to.

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [February 22, 2021, 5:13pm UTC](https://discuss.elastic.co/t/double-closing-a-transaction-in-a-try-with-resource/264920/4 "2021-02-22T17:13:53Z")

</div>

> [@b333to](#):
>
> - The framework I am using here is `JAX-RS` .
> - I am not 100% sure of what the question `Isn't it covered by our builtin instrumentation?` is referring to.

Why are you manually instrumenting these methods? Did you try running your app without any manual instrumentation and the agent did not create transactions?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 22, 2021, 5:13pm UTC](https://discuss.elastic.co/t/double-closing-a-transaction-in-a-try-with-resource/264920/5 "2021-03-22T17:13:59Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
