跪拜 Guibai
← All articles
Java · Spring Boot · Performance Optimization

Spring Boot 4.1 Leaves Virtual Threads Off by Default; Enabling Them Turns Hikari's Pool Into the Bottleneck

By Flynt ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

An upgrade that assumes virtual threads arrive by default silently keeps Tomcat on 200 platform threads, and enabling them without sizing the connection pool fails a third of requests at 1500 concurrency. Lazy connection fetching, a one-property change in 4.1, turns the same 10-connection pool into a 25x throughput gain for transactions that do non-SQL work first.

Summary

Spring Boot 4.1 keeps virtual threads disabled by default. A probe endpoint on 4.1.2 with JDK 21 returns http-nio-8080-exec-1 platform threads and virtual:false; the spring.threads.virtual.enabled property has defaulted to false since 3.2 introduced it, and 4.1.2 has not changed that. Set it to true and the handler thread becomes a VirtualThread on ForkJoinPool-1, but the pipeline beyond Tomcat changes nothing on its own.

The switch exposes HikariCP as the ceiling. At 400 concurrency both platform and virtual configurations land near 33 rps, the pool's capacity given 10 connections and a 300 ms per-request hold time, and virtual threads merely move waiting from Tomcat's pool to Hikari's queue (wait peaks of 390 versus 189). At 1500 concurrency, virtual threads fail 29.9% of requests on Hikari's 30-second connectionTimeout, while the platform-thread config returns 0% errors because Tomcat's 200-worker cap throttles concurrency before it reaches the pool.

Enlarging the pool from 10 to 50 lifts throughput from 43.7 to 170.3 rps and p99 from 12.7s to 3.6s, with all 50 connections saturated. Wrapping the same 10-connection pool in LazyConnectionDataSourceProxy defers acquisition until the first SQL statement and reaches 1107.1 rps with p99 at 1.1s, a 25x jump measured on a 1-core box. The fix only pays when transactions do non-SQL work before SQL, which real handlers do constantly.

Takeaways
— spring.threads.virtual.enabled has defaulted to false since Spring Boot 3.2 introduced it, and 4.1.2 keeps that default; the switch must be set to true explicitly.
— A probe endpoint on Spring Boot 4.1.2 with JDK 21 reports http-nio-8080-exec-1 platform-thread names and virtual:false until the property is enabled.
— At 400 concurrency on a 10-connection pool, platform and virtual configurations both settle near 33 rps, the pool ceiling for a 300 ms per-request hold time; virtual threads push the Hikari wait peak to 390 versus 189.
— At 1500 concurrency, the virtual-thread configuration fails 29.9% of requests on Hikari's 30-second connectionTimeout (waiting=1489), while the platform-thread configuration returns 0% errors because Tomcat's 200-worker default throttles requests first.
— Raising the pool from 10 to 50 connections lifts throughput from 43.7 to 170.3 rps and drops p99 from 12.7s to 3.6s with all 50 connections saturated and 350 still queued.
— Wrapping the same 10-connection pool in LazyConnectionDataSourceProxy lifts throughput to 1107.1 rps and p99 to 1.1s; the absolute number is CPU-bound on a 1-core box, but the relative gain comes from deferring acquisition to the first SQL statement.
— Spring Boot 4.1 adds official support for LazyConnectionDataSourceProxy and a spring.datasource.connection-fetch=lazy property that enables it in one line.
— Lazy fetching only shortens pre-SQL connection hold time; a transaction that executes SQL immediately gets no benefit.
— JDK 21 still pins carrier threads inside synchronized blocks; JEP 491 delivers the fix in JDK 24.
Conclusions

At 1500 concurrency, Tomcat's 200-worker default was acting as quiet backpressure; virtual threads removed that valve before the pool had spare capacity, so the queue just moved and the timeout got exposed. Removing one queue without sizing the next relocates the failure.

The 29.9% error rate is a capacity-planning failure; virtual threads made Hikari's fixed size and 30-second timeout visible faster, not introduced them.

A 25x gain from deferring connection acquisition shows how much non-SQL work sits inside real transactions. Pooled connections spend most of their hold time idle, so shrinking hold time beats buying more connections.

The widely repeated claim that Spring Boot 4.1 enables virtual threads by default is false, yet the secondary diagnosis attached to it, HikariCP as the bottleneck, holds up under load. A wrong premise costs a reader the first debugging step.

Concepts & terms
Virtual threads
JVM-managed lightweight threads that run on a small pool of carrier platform threads; a blocked virtual thread yields and lets the carrier run another, so one platform thread can host thousands of logical threads.
HikariCP
The JDBC connection pool Spring Boot uses by default. Its fixed connection count and connectionTimeout determine how many concurrent SQL operations can proceed; when all connections are held, new requests wait up to connectionTimeout before failing.
LazyConnectionDataSourceProxy
A Spring JDBC proxy that postpones obtaining a physical connection from the pool until the first actual SQL statement executes, instead of at transaction start, shortening the time each connection is held.
Pinning
On JDK 21, entering a synchronized block parks the carrier platform thread carrying the virtual thread, so synchronized code inside a virtual thread blocks a whole carrier rather than yielding. JEP 491 removes this behavior in JDK 24.
Backpressure
A mechanism upstream that limits how much load reaches a downstream resource. Tomcat's default 200 worker threads act as one, queueing excess requests at the container layer before they hit the connection pool.
From the discussion

The clarification distinguishes the direct JDK virtual-thread executor from Spring Boot's container-level switch; they share the same underlying mechanism but differ in scope. Both paths run into the same HikariCP 10-connection default when accessing a database, which the reply identifies as the source of a third of the article's 500 errors. Pool sizing should follow concurrency, not the virtual-thread enablement choice.

— The JDK API (Executors.newVirtualThreadPerTaskExecutor()) and the Spring Boot switch (spring.threads.virtual.enabled=true) operate on different layers: one for manual task submission, the other for reconfiguring Tomcat request handling and @Async; they are not interchangeable.
— No matter how virtual threads get enabled, database access still hits HikariCP's default 10-connection pool as a bottleneck, so pool capacity must be sized against actual concurrency.
Featured comments
用户1480620003153

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.

Flynt

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.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗