Why Spring Boot’s connectionTimeout Doesn’t Affect Request Time – Experiments Explained
This article experiments with Spring Boot’s Tomcat connectionTimeout setting using controller delays, HttpURLConnection requests, and raw socket connections, demonstrating that the timeout only applies when a client stays idle after connecting, not to request processing time or client type.
Environment: Spring Boot 2.5.12
application.yml configuration:
server:
port: 8081
tomcat:
maxThreads: 10
maxConnections: 10
acceptCount: 1
connectionTimeout: 3000Test 1: In a controller, sleep 10 s > connectionTimeout.
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/index")
public Object index() {
try {
System.out.println(Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "success";
}
}Result: the program responds normally.
Conclusion: The connectionTimeout parameter is unrelated to the actual request processing time.
Test 2: Send a request with HttpURLConnection.
public class HttpURLConnectionDemo {
public static void main(String[] args) throws Exception {
HttpURLConnection con = (HttpURLConnection) new URL("http://localhost:8081/test/index").openConnection();
con.setDoInput(true);
con.setDoOutput(true);
long start = System.currentTimeMillis();
InputStream is = con.getInputStream();
Scanner scan = new Scanner(is);
while (scan.hasNext()) {
System.out.println("Received: " + scan.next() + "
Time: " + (System.currentTimeMillis() - start));
}
scan.close();
con.disconnect();
}
}Result shown in the image:
Conclusion: connectionTimeout does not depend on the client type that makes the request.
Test 3: Connect via raw Socket.
public class TomcatConnectionTimeoutDemo {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 8081);
long start = System.currentTimeMillis();
InputStream is = socket.getInputStream();
is.read();
System.out.println(System.currentTimeMillis() - start);
}
}Result (≈3 s) shown in the image:
Extended test: after establishing the socket, a separate thread continuously writes data from console input while the main thread reads. When no data is sent for more than the configured connectionTimeout (3 s), the connection is closed.
public class TomcatConnectionTimeoutDemo {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("127.0.0.1", 8081);
long start = System.currentTimeMillis();
final OutputStream os = socket.getOutputStream();
new Thread(() -> {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String content = scan.next();
System.out.println("Preparing to send: " + content);
try {
os.write(content.getBytes());
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
InputStream is = socket.getInputStream();
is.read();
System.out.println(System.currentTimeMillis() - start);
}
}Result when nothing is sent (image 1) and when continuously sending input (image 2) are shown.
Final conclusion: The connectionTimeout setting controls how long the server will keep a TCP connection open after the client has connected but does not send any data; if the client remains idle longer than this timeout, the server closes the connection.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
