# Current transaction carried over ExecutionService

**URL:** <https://discuss.elastic.co/t/current-transaction-carried-over-executionservice/328959>\
**Category:** APM\
**Tags:** java\
**Created:** [March 30, 2023, 4:37pm UTC](https://discuss.elastic.co/t/current-transaction-carried-over-executionservice/328959 "2023-03-30T16:37:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![OrangeDog](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orangedog/32/4630_2.png) [@OrangeDog](https://discuss.elastic.co/u/OrangeDog)\
**Post date:** [March 30, 2023, 4:37pm UTC](https://discuss.elastic.co/t/current-transaction-carried-over-executionservice/328959/1 "2023-03-30T16:37:23Z")

</div>

I have a Spring Boot application using APM Agent 1.36.0.

A request submits a job to an executor and returns immediately. The relevant code looks like:

```java
private ExecutorService executor = Executors.newSingleThreadExecutor();

public void scheduleJob(Job details) {
    executor.submit(() -> doProcessing(details));
}

@CaptureTransaction(type = "scheduled")
protected void doProcessing(Job details) {
    // stuff
}

```

However, APM collects the spans generated during `doProcessing` under the original request transaction.

Is the transaction state supposed to be maintained across threads like this, and is there a way to (possibly selectively) stop it?

Config:

```ini
use_path_as_transaction_name=true
disable_instrumentations=experimental,micrometer
enable_log_correlation=true
span_stack_trace_min_duration=-1ms
cloud_provider=none
capture_headers=true
enabled=true
transaction_ignore_user_agents=Elastic-Heartbeat/*,Zabbix/*,*OpenVAS-VT*,*Nessus*,*MSIE 8.0*
central_config=false
environment=staging
application_packages=com.example.myapp
use_elastic_traceparent_header=false

```

---

<div class="post-metadata">

**Author:** ![Jonas\_Kunz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonas_kunz/32/110740_2.png) [@Jonas\_Kunz](https://discuss.elastic.co/u/Jonas_Kunz)\
**Post date:** [March 31, 2023, 6:50am UTC](https://discuss.elastic.co/t/current-transaction-carried-over-executionservice/328959/2 "2023-03-31T06:50:18Z")

</div>

You are correct, by default we propagate the trace context across asynchronous invocations, like `Executor` invocations.

The `@CaptureTransaction` does not start a transaction when a transaction is currently active.  
However, this restriction does not apply to [`ElasticAPM.startTransaction()`](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#api-start-transaction). Could you try this method instead?

If you want to stop trace propagation for `Executor`s completely, you can add `executor` to the [disable\_instrumentations](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-disable-instrumentations) config setting instead.

---

<div class="post-metadata">

**Author:** ![OrangeDog](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orangedog/32/4630_2.png) [@OrangeDog](https://discuss.elastic.co/u/OrangeDog)\
**Post date:** [March 31, 2023, 8:39am UTC](https://discuss.elastic.co/t/current-transaction-carried-over-executionservice/328959/3 "2023-03-31T08:39:17Z")

</div>

I see, thank you.

Perhaps there should be a docs update to link each `disable_instrumentations` option to the feature(s) it controls.

[Supported technologies | APM Java Agent Reference [1.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/java/current/supported-technologies-details.html) doesn't include the option names.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [April 4, 2023, 2:42pm UTC](https://discuss.elastic.co/t/current-transaction-carried-over-executionservice/328959/4 "2023-04-04T14:42:07Z")

</div>

Also an alternative, you could use `@Traced(type = "scheduled")` annotation to automatically create a transaction if none is already active and a child span if there is none (which allows nested invocations).

---

<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:** [April 25, 2023, 10:42am UTC](https://discuss.elastic.co/t/current-transaction-carried-over-executionservice/328959/5 "2023-04-25T10:42:12Z")

</div>

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