BBackend CourseLeroy Labs
0/0
Search the course

Connection pooling (HikariCP) & open-in-view

Why connections are pooled, how to size the pool, what happens when it runs out, and why every one of your projects sets spring.jpa.open-in-view to false.

● intermediateReport-Manager-2IR3D (Kontentt)Hostel-Web-App

Connection pooling

Opening a Postgres connection is expensive: a TCP handshake, TLS, authentication, and a whole OS process forked on the server. A pool keeps N connections open and lends them out.

Spring Boot’s default is HikariCP:

spring.datasource.hikari:
  maximum-pool-size: 20
  minimum-idle: 5
  connection-timeout: 30000     # wait at most 30s for a free connection, then throw
  max-lifetime: 1800000         # recycle before the DB or load balancer kills idle conns
  leak-detection-threshold: 20000

Sizing

Bigger isn’t better. Postgres performance drops with too many active connections because of context switching and lock contention. A classic starting formula (from the Hikari docs):

connections = (CPU cores × 2) + effective_spindle_count

A 4-core DB server needs roughly 10 connections, which handles thousands of requests per second when queries are fast.

Total connections = pool size × number of app instances. Three instances with a pool of 20 is 60 connections. Watch Postgres max_connections (default 100), especially on small managed tiers such as RDS micro or Render free Postgres.

Pool exhaustion

When every connection is busy, new requests wait up to connection-timeout, then fail with SQLTransientConnectionException. Common causes:

  • Long transactions (an HTTP call inside a transaction).
  • Connection leaks (a Connection not closed in manual JDBC).
  • Open-in-view holding connections during rendering.
  • Slow queries (missing indexes).

open-in-view

With OSIV = true (Spring Boot’s default, with a startup warning), the Hibernate session and its DB connection stay open for the whole HTTP request, including JSON serialisation, so lazy loading “just works” in the controller and view.

The problems:

  1. Hidden N+1 queries fire during serialisation, outside your service.
  2. Connections are held longer, e.g. while streaming a big response, so the pool drains under load.
  3. The service layer no longer defines what data is loaded.

All your projects set:

spring.jpa.open-in-view: false

As a result, lazy access outside a transaction throws LazyInitializationException, which forces you to fetch deliberately (JOIN FETCH, @EntityGraph, DTO mapping inside the service).

In your projects

Report-Manager-2application-prod.yaml

Hikari is set to 20 max / 5 idle in prod, and open-in-view: false.

IR3D (Kontentt)application.yaml · application-prod.yaml

The pool is 10 by default and DB_POOL_SIZE:15 in prod (configurable per environment, since EC2 instance size and RDS limits differ).

Hostel-Web-Appapplication.yaml (server.tomcat.*)

Hostel also tunes the Tomcat side (max threads, connection limits). Tomcat threads should be at least as many as the DB pool, but a 200-thread Tomcat with a 10-connection pool means up to 190 requests can queue for a connection.

Alternatives / complements

Tool Notes
HikariCP (yours) Fastest JVM pool; the Spring default
PgBouncer An external pooler in front of Postgres; essential with many app instances or serverless
RDS Proxy AWS-managed pooler
R2DBC pool For reactive (WebFlux) apps
  • Virtual threads don’t remove the pool limit. 10,000 virtual threads still share about 20 connections, so the pool becomes the bottleneck, which is fine and intended.
  • Read replicas with a routing DataSource (readOnly transactions go to the replica).
  • Metrics. Watch hikaricp_connections_active and hikaricp_connections_pending. You already export Prometheus metrics in RM and IR3D.

Check yourself

1. 3 app instances × Hikari max 20. Max DB connections used?
2. Main reason to disable open-in-view?