Connection pools and retries
What you will learn
A connection pool bounds concurrent database sessions and reuses connections. A retry policy must classify failures, limit attempts, and open a fresh transaction for each attempt. Neither an enormous pool nor unlimited retry improves reliability.
- Explain pool capacity and acquisition timeout.
- Classify retryable failures without retrying ambiguous commits blindly.
- Explain replica lag and read-your-writes.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. PostgreSQL and Spring examples require the companion lab or configured infrastructure. Their predicted results are not execution evidence.
Bound concurrent permits
public class Main {
public static void main(String[] args) {var permits=new java.util.concurrent.Semaphore(2); boolean a=permits.tryAcquire(),b=permits.tryAcquire(),c=permits.tryAcquire(); System.out.println(a+" "+b+" "+c); permits.release();System.out.println(permits.tryAcquire()); }}Expected output:
true true falsetrueA semaphore models admission, not a connection pool; no driver or SQL involved.
Pool and timeout budgets
Borrow from a DataSource and close promptly using try-with-resources; a pooled close usually returns a logical lease. Set acquisition, connection, statement, lock and request deadlines separately; keep retries within the overall budget. Estimate concurrent demand from request rate and time holding a connection, then measure database saturation; cap the sum of pool sizes across replicas and reserve capacity for administration. REQUIRES_NEW can require an extra connection while the outer one remains held; avoid starvation by design. Read replicas can lag, so route read-your-writes to the primary or use an explicit consistency mechanism. Replicas are not a free substitute for primary transaction isolation.
Retry policy
PostgreSQL 40001 means serialization failure; 40P01 means deadlock victim. Both can justify bounded whole-transaction retry after rollback with exponential backoff and jitter. 23505 is uniqueness violation and generally a business conflict unless a carefully analyzed race applies. Connection errors around COMMIT may leave outcome unknown; use durable idempotency and reconcile rather than repeat a side effect blindly. Never sleep while holding a connection or locks; do not retry inside an already-aborted transaction. Keep the retry wrapper outside the transaction boundary.
Classify retry codes
public class Main { static boolean retry(String state){return "40001".equals(state)||"40P01".equals(state);} public static void main(String[] args) {for(String code:java.util.List.of("40001","40P01","23505","08006"))System.out.println(code+" "+retry(code)); }}Expected output:
40001 true40P01 true23505 false08006 falseA conservative classifier for a known aborted transaction. A connection failure may have an unknown commit outcome.
Example pool settings
Source-reviewed, NOT RUN. File lab-fixtures/database-architecture-v43/pool.properties; use the companion lab README.
# Illustrative Spring Boot HikariCP settings, NOT RUN.spring.datasource.hikari.maximum-pool-size=4spring.datasource.hikari.connection-timeout=1000# Pool wait timeout is not a SQL statement timeout.Predicted behavior: Four maximum leases and a one-second acquisition budget if HikariCP is active.
No Spring pool instantiated.
Common mistakes
Do not retry every SQLException, especially after a possibly successful commit. Never retain a connection across retry delays, and do not route a newly committed read to a lagging replica when freshness is required.
Practice
Simulate three failed attempts with SQLSTATE 40001 and a maximum of three total attempts. Then simulate 23505 on the first attempt. Print attempts for both, without real sleeping or database calls.
Expected output:
31Compare with the manual review criteria.
Official reading: Retry · Locks · Propagation · JDBC. PostgreSQL and Spring execution and independent review remain pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Connection pools and retries — knowledge check
Database transaction and data architecture reasoning.
Does a pool acquisition timeout bound SQL execution time?
What is a safe retry boundary for a 40001 failure?
Why is connection loss around COMMIT special?
What can happen if every worker holds an outer connection then requests REQUIRES_NEW?
A user reads immediately after a write. Which route is safest for read-your-writes?