Using threadlocals on async requests

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Using threadlocals on async requests

Thiago Veronezi
Hi community,

I have a java agent that has its own transaction feature and it does it by
using Threadlocals. The agent transactions feature seems to work ok with
Tomcat async requests.

Does anyone know it's fine to use threadlocals on async requests?

Thanks!
Thiago.
Reply | Threaded
Open this post in threaded view
|

Re: Using threadlocals on async requests

Christopher Schultz-2
Thiago,

On 3/9/21 08:17, Thiago Veronezi wrote:
> I have a java agent that has its own transaction feature and it does it by
> using Threadlocals. The agent transactions feature seems to work ok with
> Tomcat async requests.
>
> Does anyone know it's fine to use threadlocals on async requests?

It's definitely *not* fine to use ThreadLocal with asynchronous
requests. If it's working for you, its only due to some very lucky
circumstances.

If you need to store information between invocations of IO callbacks,
you will need to store that information in the request/response objects.

-chris

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Using threadlocals on async requests

Thiago Veronezi
Yes. I noticed that. :)
I was only testing with the ByteCounter example that comes with tomcat.That
example uses nio, so there's no new thread. As soon as I use...

```
val ac = req.startAsync();
ac.start(() -> {
   // <logic in here>
});
```
The runnable does not get the threadlocal, obviously. :)

Thanks for the feedback Chris!

[]s,
Thiago.


On Tue, Mar 9, 2021 at 3:03 PM Christopher Schultz <
[hidden email]> wrote:

> Thiago,
>
> On 3/9/21 08:17, Thiago Veronezi wrote:
> > I have a java agent that has its own transaction feature and it does it
> by
> > using Threadlocals. The agent transactions feature seems to work ok with
> > Tomcat async requests.
> >
> > Does anyone know it's fine to use threadlocals on async requests?
>
> It's definitely *not* fine to use ThreadLocal with asynchronous
> requests. If it's working for you, its only due to some very lucky
> circumstances.
>
> If you need to store information between invocations of IO callbacks,
> you will need to store that information in the request/response objects.
>
> -chris
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>