Does Spring Boot’s connectionTimeout Limit Request Time? The Real Answer
This article investigates how Spring Boot’s connectionTimeout parameter behaves by conducting three experiments—controller‑side delay, HttpURLConnection requests, and raw socket connections—revealing that the timeout only applies after a client establishes a connection and remains idle, not to the overall request processing time.
Environment: Spring Boot 2.2.11.RELEASE
application.yml configuration:
<code>server:
port: 8081
tomcat:
maxThreads: 10
maxConnections: 10
acceptCount: 1
connectionTimeout: 3000
</code>Test 1: In a controller, sleep 10 seconds > connectionTimeout.
<code>@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";
}
}
</code>Result: The program responds normally.
Conclusion: The connectionTimeout parameter is unrelated to the actual request processing time.
Test 2: Send request using HttpURLConnection.
<code>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 content: " + scan.next() + "\nTime elapsed: " + (System.currentTimeMillis() - start));
}
scan.close();
con.disconnect();
con = null;
}
}
</code>Result:
Conclusion: The connectionTimeout parameter does not depend on which client makes the request.
Test 3: Establish connection via raw Socket.
<code>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 );
}
}
</code>Result:
After about 3 seconds the program ends, indicating the connection was closed.
Extended test with continuous console input:
<code>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 );
}
}
</code>Result when doing nothing:
Result when continuously entering data in console:
Initially the program runs and accepts input continuously; after a 3‑second pause without input, the connection is closed.
Final conclusion: The connectionTimeout parameter defines the period after a client has established a TCP connection during which, if no data is sent, the server will close the connection; it does not limit the total request processing time.
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.