Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
1000 requests that each wait 500 ms take 2762 ms on Tomcat's 200 threads and 1077 ms with one property for virtual threads. Then the trap: with a pool of 10 connections held 100 ms each, 300 requests crawl at 98 per second, however many threads wait.
What you will learn
- Turn on virtual threads for every request with spring.threads.virtual.enabled
- Explain why a waiting virtual thread costs almost nothing (carriers and parking)
- See the Hikari connection pool become the real limit under virtual threads
- Size a pool from requests per second times seconds held
Before you start
- Why Slow Work Belongs off the Request Thread
- Connect Spring Boot to PostgreSQL: Driver, application.yml and the Hikari Pool
With one line in application.properties, a Spring Boot 4.1 app can wait on a thousand slow calls at once. Then the connection pool quietly decides how many can really work. Let's measure both: the property, the numbers before and after, and then the pool.
A thread that only waits
A small ticket desk. GET /tickets/{id} calls the service and adds an X-Thread header with the thread that handled the request. The service stands in for a slow call to another system:
@Service
public class TicketService {
private final DataSource dataSource;
public TicketService(DataSource dataSource) {
this.dataSource = dataSource;
}
// A slow call to another system: the thread only waits
public Ticket load(long id) throws InterruptedException {
Thread.sleep(500);
return new Ticket(id, "Printer jammed");
}
// A database call: it keeps one connection for the whole query
public Ticket loadFromDatabase(long id) throws Exception {
try (Connection connection = dataSource.getConnection()) {
Thread.sleep(100); // stands in for a slow query
return new Ticket(id, "Printer jammed");
}
}
}
In load, the thread does no work. It only waits half a second.
1000 requests on 200 platform threads
Without virtual threads, Tomcat has 200 request threads. A thousand requests arrive within half a second: 200 get a thread, 800 wait outside. After 500 ms the first 200 are done and their threads take the next 200. Five rounds of half a second, about 2.5 seconds, and every thread spends its round doing nothing. A test that really starts Tomcat (with @LocalServerPort) and fires the load, with spring.threads.virtual.enabled=false:
virtual threads: off
1000 requests in 2762 ms: 1000 ok, 0 failed (362 per second)
threads that answered: 200
one request ran on Thread[...http-nio-auto...]
One line in application.properties
spring.threads.virtual.enabled=true
spring.datasource.hikari.maximum-pool-size=10
Line one makes Tomcat give every request its own virtual thread. Line two is Spring Boot's default pool size, written out so you keep it in mind. No Java changes at all. The same thousand requests:
1000 requests in 1077 ms
threads that answered: 1000
one request ran on VirtualThread[...]/ForkJoinPool-1-worker-N
Less than half the time, and a thousand different threads answered.
Why waiting costs almost nothing
A virtual thread mounts a carrier and runs its code. When it reaches sleep (or a blocking call), it steps off, and the carrier takes the next virtual thread. A parked virtual thread costs almost nothing, so hundreds can wait together. When the wait is over, it mounts a carrier again and finishes.
The trap: a thread that holds a connection
Now the second method, loadFromDatabase. It takes a connection from the Hikari pool, holds it for 100 ms (a stand-in for a slow query), and gives it back when the try block closes. If all ten connections are in use, the thread waits right there on getConnection(). Three hundred requests to the database endpoint, virtual threads on, pool of 10:
connection pool: 10
300 requests in 3060 ms: 300 ok, 0 failed (98 per second)
All 300 virtual threads start at once, because threads are cheap. But only 10 can hold a connection; the other 290 wait for one. Thirty rounds of 100 ms, about three seconds. It can get worse: with the connection timeout set to one second, 180 of the 300 requests failed with:
HikariPool-1 - Connection is not available, request timed out after 1001ms
Virtual threads are cheap. A connection is not.
A bigger pool, a higher ceiling
spring.datasource.hikari.maximum-pool-size=50
connection pool: 50
300 requests in 981 ms (305 per second)
Same code, five times the pool, about a third of the time.
Pool size sets the ceiling
The most requests per second you can get is connections ÷ seconds each one is held:
| Pool | Ceiling (held 0.1 s) | Measured |
|---|---|---|
| 10 | 100 per second | 98 per second |
| 50 | 500 per second | 305 per second |
Turn it around to size a pool: connections needed = requests per second × seconds held. For 300 requests per second at 0.1 s each, that's 30 connections. Then check two limits: the database's own maximum, and the fact that every copy of the app has its own pool. In the PostgreSQL article the server allowed 100 connections in total.
Common mistakes
- Expecting virtual threads to speed up the database. They make waiting cheap. The pool still caps the work.
- Setting the pool to 1000 "to match the threads". The database server can't take it, and every app copy multiplies it.
- Holding a connection while calling another slow system. Release the connection first; it's the scarce thing.
- Measuring with a test that doesn't start Tomcat. Use a real port so the request threads are real.
Try it yourself
With a pool of 10 and each query held for 50 ms instead of 100, what ceiling does the formula give? Change the sleep, run the 300 requests, and compare.
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Why Spring in 2026: What the Container Does and What Spring Framework 7 Changed
Spring in one line: a container that creates your classes and hands each one what it needs. Plus the Spring Framework 7 baseline (JDK 17 to 25, Jakarta EE 11, Tomcat 11, JUnit 6), what got better than Spring 6, and a two-line version check.
A Plain Spring Project, No Boot: Your First Bean With spring-context
One dependency, one recipe, one getBean: a Maven project with spring-context 7.0.9, a Blog record, an AppConfig with @Bean, and a main that asks the container for the Blog. Then delete @Bean and meet NoSuchBeanDefinitionException.
Inversion of Control and Dependency Injection in Spring: One Program, Built Three Ways
A PostService needs a PostRepository. Build it three ways: the service calls new itself (tight coupling), main hands it in through the constructor (DI by hand), and the Spring container does the wiring from a @Configuration recipe.
Spring Component Scanning: @Component and @ComponentScan Instead of @Bean Methods
Instead of one @Bean method per class, put @Component on EmailNotifier and PostService and @ComponentScan on an empty BlogConfig. Spring walks the package, finds the stickers and builds the beans; main doesn't change.
