New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Java 21, Virtual Threads and ThreadLocal #2518
Comments
ThreadLocal is supposed to be fully supported by virtual threads, so I agree that since we limit it by number of DB connections, it should be fine. Scoped values sound cool but I think it's too early to consider them. |
Hello, |
We hope that it just works, but I haven't personally tested it yet. If you do run it and find any problems please let us know and they should get fixed. |
I did some testing, if I integrate jdbi with Quarkus and run without VT (I created a demo project with a REST endpoint and a JMeter client calling it with 100 threads), everything works correctly. If I annotate the resource to run on a VT with
... zip ...
|
Thanks for giving this a try - unfortunately, the stack trace provided isn't enough to diagnose the problem. Any chance you could post your sample project? |
And it is expected that right now Jdbi would pin carrier threads potentially - we do use If the project turns into rewriting all |
I will create a minimal project that reproduces the problem |
@stevenschlansker you can find a self-contained test project attached. Running it with JMeter with 100 threads POSTing on localhost:8080 this request body JSON:
triggers the problem. If you remove the |
Hi @vszakd , I downloaded and imported your project into Eclipse, but I cannot figure out how to run it. None of the classes have a |
I created: |
Ok, I made a slightly more complex test that runs 100 virtual threads over 10 connections, and it doesn't work as well. So at least I have something to work on now. |
I pushed my failing test up to the branch. |
Well, the good news is the initial Loom programming experience is great, but unfortunately the observability / debuggability seemingly still sucks. I am able to see the program hang, but I do not see any pinned threads (tried using |
Hello @stevenschlansker, it's a Quarkus application so it does not have an explicit Before you can use the If you prefer, I can upload a maven project. |
Jdbi at this point has one relevant ThreadLocal instance, which is a central one:
We maintain a reference to the "current handle supplier" in Jdbi#threadHandleSupplier. This enables the ability to bind extension objects in
Jdbi#withExtension
and Handles inJdbi#withHandle
and transparently keep using the same handle even in recursive calls as long as these operations are done on the same thread.We do not use the "threadlocal as cache" scenario as described in JEP 429/446, so it is not clear whether there is real pressure to remove the thread local. Even with thousands of virtual threads, the number of concurrent instances will be limited by e.g. the number of concurrent database connections supported, which is a pretty limited resource. A ThreadLocal with a few hundred instance s that all get removed don't seem to be a big deal. The code cleans out the ThreadLocal instances by calling
#remove()
so the ThreadLocal itself should not grow unbounded.JEP 446 offers scoped values as an alternative. However, as of Java 21 LTS, scoped values have not been shipped, so we would need to build a multi-version jar to support it.
There actually is a second ThreadLocal, in the StringTemplate loader/cache mechanism; this should be easily replaced with a cache using the core cache facility.
The text was updated successfully, but these errors were encountered: