Fix RestTemplate with no timeout
Free Spring Boot lab: RestTemplate hangs with no connect or read timeout and pins servlet threads. Set timeouts in the browser — no sign-up.
Checkout hangs when the payment provider stalls. Thread dumps show BLOCKED on socketRead. The default RestTemplate has infinite connect and read timeouts, so one slow upstream can pin the whole servlet pool.
PaymentClient builds new RestTemplate() with SimpleClientHttpRequestFactory and no timeouts. When payments.example.com never responds, every charge request holds a Tomcat thread forever. Recovery often needs a restart even after the provider recovers.
The lab asks you to set connect and read timeouts (and prefer a shared client). Increasing thread pool size without timeouts only delays the outage. This is the HTTP-client version of the HikariCP leak: a resource you borrowed and never bounded.
What you will cover
- Symptom: requests hang forever; dump shows socketRead
- Default RestTemplate connect/read timeout is infinite
- Root cause: no RequestFactory timeouts on the HTTP client
- Wrong fix: add more Tomcat threads
- Right fix: connect, read, and connection-request timeouts
Related free labs and modules
Common questions
Why does RestTemplate hang with no timeout in Spring Boot?
The default SimpleClientHttpRequestFactory leaves connect and read timeouts at infinity. A slow or dead upstream holds the calling thread until the process restarts. Production clients must set explicit timeouts.
What timeouts should I set on RestTemplate?
Set connect timeout, read timeout, and connection-request timeout on the client HTTP request factory. Typical production values are a few seconds, not minutes. Prefer a shared RestTemplate or WebClient bean, not new RestTemplate() per call.
Is the RestTemplate timeout lab free?
Yes. You can open this Spring Boot HTTP-client lab without an account. It stays free for life in the browser.
Free Spring Boot debug challenges · Free debug challenges · Java Spring Boot path · Create free account