FATAL: sorry, too many clients already
That is PostgreSQL's reply when one more connection arrives than its max_connections setting allows, 100 by default. It tends to appear the week an API that opens a fresh connection for every request gets popular. Each of those connections is expensive twice over. Opening one takes a TCP handshake, often a TLS negotiation, authentication, and a new process on the server, since PostgreSQL runs a backend process per connection: milliseconds of work before the first query, paid again on every request. And the database holds only so many at once, so a spike of requests becomes a spike of connections, and past the limit the next one is refused.
Object Pool answers both with one idea: create the expensive objects once, keep them, and lend them out. A request borrows (acquires) a connection from the pool, runs its queries and returns (releases) it, and the next request reuses that connection without paying to open it. The pool also caps how many exist, so the database never sees more connections than the pool allows. The pattern lives or dies in the details of one connection's life, so follow one.
The client never opens a connection itself. It borrows one from the pool, which keeps a capped set of connections open to the database.
Created on demand, up to a cap
A pool does not have to open every connection at start-up. A common setup keeps a few idle connections ready; when a request arrives and none is free, the pool creates a new one, as long as the total stays under the maximum, say 20. At the cap it creates nothing more, and the borrower waits until someone returns a connection.
Counting borrowers against a fixed number of connections is what a semaphore does, and Mutex and semaphore shows the same ten-connections-for-many-threads problem from the locking side; a pool does that counting for you and adds the reuse. The wait at the cap needs a limit, though. A borrower that waits forever keeps holding what it already has, a request thread and a user's open HTTP call, while more borrowers pile up behind it. A borrow that gives up after, say, two seconds turns an exhausted pool into a fast, visible error instead of a hung service.
Returned dirty
Request A borrows connection 3 and runs SET search_path TO tenant_42, outside any transaction, so the setting stays for the whole session. It then begins a transaction and inserts two rows, and its own code throws before the commit. The error handler returns connection 3 to a pool that does no reset. Request B, for another tenant, borrows connection 3 next. Its queries run against tenant 42's schema, inside A's open transaction, and when B commits, A's two half-finished rows commit with it.
A pooled object keeps every bit of state its last borrower left on it, and the next borrower inherits it. So the object has to be reset before it is lent again: the open transaction rolled back, the changed settings put back. An object whose reset fails is closed rather than lent, which frees its slot for a new one. Many pools roll back a transaction left open, but a setting changed with plain SQL can slip past them, so check what yours actually resets. Every kind of pooled object needs its own reset. A reused byte buffer still holds the previous request's bytes, and code that reads past what it wrote sends them to the wrong user. Reset is the step that makes reuse safe; skipping it puts one user's data in another user's request.
Never returned
The opposite failure is quieter. A code path borrows a connection and throws before the line that returns it. The pool still counts the connection as borrowed, and nothing will ever give it back. Each such error leaks one connection; after 20 of them every borrow waits until it times out, and the service is down while the database sits idle. (A connection can also go bad on the shelf, when a firewall drops an idle session, which is why pools check connections and retire old ones.) The cure is the one for any lock: return on every path out, with try/finally or the language's scoped form, such as Java's try-with-resources or Python's with.
How big, and how many pools
Size the pool to what the thing behind it can take, not to how much traffic arrives. Four app instances with a pool of 50 each can open 200 connections, and a database that accepts 100 refuses every connection past its limit: the error the pool was meant to end. The sum of every instance's maximum has to fit under the database's limit, with room left for migrations and admin sessions. A bigger pool is not more throughput either. The database runs only so many queries in parallel before they compete for its CPU and disks, and connections beyond that point queue inside the database instead of in front of it. An unbounded pool just moves the overload downstream.
One pool shared by several dependencies also spreads failures: if every outbound HTTP call borrows from the same pool, with no limit per destination, and a payment provider slows to ten seconds per call, payment calls end up holding every connection and unrelated requests wait behind them. Giving each dependency its own pool is the Bulkhead pattern.
What else belongs in a pool
Anything costly to create and safe to reuse by one borrower at a time: HTTP connections, large buffers, and threads, whose pool adds a task queue and questions of its own that Thread Pool covers. Cheap objects do not belong: in a garbage-collected runtime a small, short-lived allocation usually costs less than a pool, which would only add the reset and leak bugs above. And a pool is not Flyweight, which shares one immutable object among many users at once; a pool lends one, usually stateful, to a single borrower at a time and expects it back.
Lend, reset, return, on a Grid
The Object Pool Pattern challenge asks for that whole life on one drawing. Use a Client or two, the Object Pool, and Component nodes for the pooled connections, with edges labelled acquire, use and release. Show the pool creating a new connection only when none is free, with the cap in its title, such as "max 20". The reset is the requirement drawings most often leave implicit: give it its own step between release and the next borrow, because that step is what stops tenant 42's settings from reaching the next user.