Spring Boot 4.1 Leaves Virtual Threads Off by Default; Enabling Them Turns Hikari's Pool Into the Bottleneck
Recently, while browsing Juejin, an article about Spring Boot 4.1 virtual threads sat in the recommended feed. Its core claim: upgrade to 4.1 and virtual threads turn on automatically, making HikariCP the bottleneck. I happened to have an old service scheduled for a 4.x upgrade, and virtual threads is a feature I've wanted for a long time, so I couldn't resist running the verification first — the first thing being to verify the "automatic enabling" claim.
The result was a bucket of cold water.
Conclusion first: it's off by default
My setup was Spring Boot 4.1.2 (current stable; 4.1.0 went GA on June 10 this year, 4.2 is still in RC2) plus JDK 21.0.13 (Temurin). The project just had one probe endpoint:
@GetMapping("/vt")
public Map<String, Object> vt() {
Thread t = Thread.currentThread();
return Map.of("virtual", t.isVirtual(), "thread", t.toString());
}
One request on the default configuration returns:
{"virtual":false,"thread":"Thread[#19,http-nio-8080-exec-1,5,main]"}
http-nio-8080-exec-1 is the standard platform-thread naming; virtual is false. I also checked the configuration metadata and the official migration guide and got the same answer: spring.threads.virtual.enabled has defaulted to false since the day it was introduced in 3.2, and through 4.x to 4.1.2 it hasn't changed. The official documentation's wording is blunt — neither 3 nor 4 activates it by default; to enable it you set it to true yourself.
So the hot article's premise doesn't hold: upgrade to 4.1 without configuring anything and virtual threads are simply not on. CI is all green, the service runs as usual, you think you're using virtual threads, but actually Tomcat is still using those 200 platform threads. Not fatal, but you won't hit any of the "new virtual thread pitfalls" either — because you never got on board. The switch is just one line:
spring.threads.virtual.enabled=true
Restart and hit /vt again; the thread name becomes VirtualThread[#22,tomcat-handler-0]/runnable@ForkJoinPool-1-worker-1. Now you're really on board.
After enabling it, the numbers slapped me
Verifying the switch wasn't enough. I built a minimal experiment: Spring Boot 4.1.2 + in-memory H2 + HikariCP (the 4.1.2 fat jar carries 7.0.2, visible in the startup logs), running on a small 1-core 2G machine, with the load generator a small Python asyncio script. The endpoint looks like this:
@GetMapping("/query")
public Map<String, Object> query() {
return tx.execute(status -> {
try {
Thread.sleep(300); // simulate non-SQL work inside the transaction: cache lookups, remote calls
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Integer one = jdbc.queryForObject("select 1", Integer.class);
return Map.of("ok", true, "virtual", Thread.currentThread().isVirtual());
});
}
I deliberately put 300 ms of "non-SQL work" at the start of the transaction, then execute a very fast SELECT 1. This isn't a contrived scenario — transaction methods that check a local cache first, validate parameters, and only then hit the database are everywhere in real projects. One side note: the connection is acquired as soon as the transaction starts, so during those 300 ms the connection is held. That detail matters later.
Control group A was the default configuration (platform threads + pool of 10), 400 concurrency for 35 seconds; group B enabled virtual threads, everything else unchanged.
| Group | Config | Concurrency | Throughput* | Error rate | p99 latency | Hikari peak wait |
|---|---|---|---|---|---|---|
| A | Platform threads, pool 10 | 400 | 42.0 | 0% | 24.0s | 189 |
| B | Virtual threads, pool 10 | 400 | 43.7 | 0% | 12.7s | 390 |
- Throughput = completed requests ÷ 35-second window. Long-tail requests in both A and B finished outside the window; the actual steady-state throughput is about 31–33 rps, which exactly hugs the pool ceiling of "10 connections ÷ 300 ms per request ≈ 33 rps."
There it is: throughput from 42 to 43.7, basically unchanged. Virtual threads supposedly handle a million concurrent connections — why did it fail here? That 42 I initially suspected was my own calculation error — the pool ceiling was clearly 10 ÷ 0.3 ≈ 33 rps, so how could the load test produce 42? Going back to the script, I found the counting problem: requests still queued in the pool when the window closed were counted in the total as they completed, but the denominator only counted 35 seconds. Recomputing with the long tail inside the window, both groups hug the 33 rps line — exactly the pool's capacity ceiling. From day one the bottleneck wasn't threads, it was connections. Enabling virtual threads just swapped "waiting in Tomcat's thread pool" for "waiting in Hikari's wait queue" — group A's queue peaked at 189, group B at 390, neither more nor less, exactly the number queueing.
At this point I thought the conclusion was "all that for nothing" and was about to wrap up. Reluctant to leave it there, I pushed concurrency to 1500. That decision made my blood pressure rise.
Push the concurrency up and the real pit appears
Group B, same config, 1500 concurrency for 45 seconds, 29.9% error rate — nearly a third of requests returned 500 directly. The logs were flooded with the same exception:
java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available,
request timed out after 30000ms (total=10, active=10, idle=0, waiting=1489)
Translated: all 10 connections were held, 1489 virtual threads were queueing, and after 30 seconds none had gotten a turn, so Hikari killed the request per the default connectionTimeout. This is the virtual-thread version of "crashes in the first round of load testing," except the crash point isn't virtual threads, it's the connection pool behind them.
The interesting bit is group A — platform threads, same 1500 concurrency, same duration, error rate 0. The reason isn't mystical: Tomcat's default of 200 worker threads means requests beyond 200 queue at the container layer, a natural backpressure valve that effectively caps concurrency at 200 and keeps queueing time inside the 30-second timeout.
Virtual threads tore that valve out. 1500 requests all became active transactions at once, each grabbing a connection at transaction start, and each missing one hangs on Hikari's wait queue. The system went from "bounded concurrency" straight to "unbounded concurrency," then got ruthlessly killed by the pool's capacity. A lot of discussion around virtual threads focuses on thread-level issues like synchronized pinning, but in my round of measurements, the thing that broke first was a more ordinary resource — and one that usually looks "good enough."
Treating the symptom: enlarge the pool, but connections cost money
The first instinct is, naturally, to adjust the pool. Group C raised the pool from 10 to 50, nothing else changed: throughput went from 43.7 to 170.3, a 4x jump; p99 dropped from 12.7s to 3.6s; nearly 6000 requests with zero failures. Effective, but look at the pool's active peak — 50, the pool was fully consumed, with 350 still waiting in the queue. Converting: every connection is being burned for 300 ms at a time, and that accounting is real money on an actual database: MySQL's per-connection memory, PostgreSQL's process overhead — none of it is something you multiply by 5 without thinking. Enlarging the pool saves you in a pinch, provided you've done the math on the line "pool size × per-request hold time ≥ target throughput."
Could the money be avoided? Group C's numbers gave me a second idea: the pool's 50 connections were occupied the whole time, yet the endpoint's actual SQL work was only the final 1 ms; for the first 300 ms the connections were just accompanying my sleep. What if the connection could be taken later?
Don't enlarge the pool; push the acquisition moment later
Group D is exactly that idea — pool back to 10, only one addition: LazyConnectionDataSourceProxy. This class is an old face in Spring JDBC; it wasn't born in 4.1. What changed in 4.1 is that Boot formally brought it into official support (the 4.1.0-RC1 announcement lists "Support for LazyConnectionDataSourceProxy"), and the manual bean-registration usage has been available in older versions all along. This is how I measured it locally on 4.1.2:
@Bean
@Primary
public DataSource txDataSource(HikariDataSource real,
@Value("${app.lazy:false}") boolean lazy) {
return lazy ? new LazyConnectionDataSourceProxy(real) : real;
}
The principle in one sentence: the connection is no longer grabbed immediately when the transaction starts; it's deferred until the first actual SQL statement. For an endpoint like /query where the transaction does 300 ms of non-SQL work at the start, the connection-hold window collapses from 300 ms to the millisecond scale of a single SELECT 1. A side note: from 4.1 the official path has a more convenient switch — spring.datasource.connection-fetch=lazy — one line of config makes Boot wrap the pool in a lazy proxy for you; you can use it directly instead of writing the bean by hand. Results:
| Group | Pool | Connection acquisition | Throughput* | p99 | Hikari peak wait |
|---|---|---|---|---|---|
| B | 10 | Immediate at transaction start | 43.7 | 12.7s | 390 |
| D | 10 | At first SQL | 1107.1 | 1.1s | 0 |
Same 10-connection pool, throughput from 43.7 to 1107, waiting queue peak to zero. Let me get the caveat out first: the absolute value of 1107 is inflated — the load generator and the Java service share one 1-core CPU, so this is the machine's ceiling, not the lazy-connection limit. But the relative number is solid — without adding a single connection to the pool, throughput rose 25x.
To be honest: lazy connections are not a free panacea. They only shorten the hold time "before the first SQL statement." If your transaction method runs SQL immediately, lazy connections buy you nothing; any waiting after the first SQL statement still occupies the connection. It treats the disease in my experiment where the transaction does other work first — and that disease is extremely common in real business: read Redis first, call downstream first, compute a signature first, all of it counts.
Two notes on the side
The pit itself is written up; two small footnotes remain. One is pinning: on JDK 21, synchronized blocks still pin the carrier thread; the fix (JEP 491) doesn't land until JDK 24. New projects can go straight to JDK 25 LTS; old projects should confirm their version before talking about virtual threads. The other is that the hot article's HikariCP analysis was actually pointed in the right direction — the pool is the bottleneck and the pending metric deserves monitoring; I reproduced all of that in my measurements. Where it went wrong was the "enabled by default" premise; a reader following it to check their own service gets confused at the first step.
My old service's final configuration: virtual threads enabled, pool sized by the formula above, and methods that do non-SQL work at the start of a transaction uniformly wrapped in a lazy connection proxy. Before wrapping up, a colleague asked whether I wanted to raise the pool to 50 as well. I calculated the memory of the database at the backend and decided against it — if connections can be saved by deferring acquisition, why pay money to buy them.
Top 2 of 3 from juejin.cn, machine-translated. The original thread is authoritative.
I directly use Executors.newVirtualThreadPerTaskExecutor() and it works. Your switch is different, right? When you enable it, Tomcat's request threads will proactively convert to virtual threads, but mine just works directly.
Right, those two are not the same thing. Executors.newVirtualThreadPerTaskExecutor() is the standard JDK 21 API, you submit tasks yourself, and using it directly is fine; spring.threads.virtual.enabled=true governs another layer—making Tomcat's request-handling threads and auto-configuration points like @Async switch to virtual threads. Boot doesn't enable it by default, and what it does is essentially the same as your manual new, just scoped to the container. One more thing: virtual threads, regardless of which way you enable them, as long as the task touches the database, the HikariCP default 10-connection pitfall is unavoidable. In my article, 30% of the 500 errors came from exactly that. Remember to calculate the pool size based on concurrency.
Must give a like! I saw that other article last time, seems yours is more reliable! 👍