Implement a simple database connection pool.
You are given a mock database client with a function such as db.query(connection, sql) or an equivalent simulated query API. Complete the connection-pool logic so callers can safely acquire and release database connections.
Requirements:
-
The pool has a fixed maximum number of database connections.
-
acquire()
returns an available connection if one exists.
-
If no connection is available but the pool has not reached its maximum size, create a new connection and return it.
-
If the pool is at capacity and all connections are busy,
acquire()
should wait until a connection is released.
-
release(connection)
returns a connection to the pool and wakes one pending waiter, if any.
-
Prevent invalid releases, double releases, and connection leaks.
-
The implementation should be safe under concurrent requests.
Discuss edge cases such as query failures, timeouts while waiting for a connection, and graceful shutdown of the pool.