Exceeded maximum connections.
`self._connections` and `self._in_use` should be mutually exclusive, though, right? i.e., the pool of connections we are pulling the connection /from/ should not be in use. The second branch there, the `elif key in self._in_use:` is there to pre-emptively remove stale connections on close, i.e. if the conn was very close to being stale when checked out, then while in use it became stale, we can simply close it when it's checked back in.

Are you sure you are not forgetting to close a connection somewhere in your app?
 Hmm but I think second branch `elif key in self._in_use` won't be invoked since we have 

``` python
def _close(self, conn, close_conn=False):
        key = self.conn_key(conn)
        if close_conn:
            self._closed.add(key)
            super(PooledDatabase, self)._close(conn)

       # this one won't be invoked because close_conn is true
        elif key in self._in_use:
            ts = self._in_use[key]
            del self._in_use[key]
            if self.stale_timeout and self._is_stale(ts):
                logger.debug('Closing stale connection %s.', key)
                self._close(conn, close_conn=True)
            else:
                logger.debug('Returning %s to pool.', key)
                heapq.heappush(self._connections, (ts, conn))

```

Main thing is "where do we remove connection from _in_use collection" because check on this throws error I'm facing.
 Can you share some of the code you're using to connect and close the conn? Or some more details about your app? The pool is covered by unit tests, so I'm not sure what I'm missing here.

My point is that I figured `self._connections` and `self._in_use` are mutually exclusive.
 Hey, sorry for bugging you earlier. The problem is on our end, we removed stale time and still have same error. I can't provide you code samples because peewee is in quite many places in our project.
Sorry for shooting from the hip.
Cheers,
Konrad

We're using PooledPostgresqlExtDatabase in our application but it looks like it doesn't clean closed connections what leads to error from the subject. After digging a little in the PooledDatabase class i think the problem is when connection is stale. Then _connect invokes _close with parameter close_conn=True

``` python
if self.stale_timeout and self._is_stale(ts):
     logger.debug('Connection %s was stale, closing.', key)
     self._close(conn, True) # HERE
     ts = conn = None
```

and later in _close function in the same class we have 

``` python
if close_conn:
    self._closed.add(key)
    super(PooledDatabase, self)._close(conn)
```

which doesn't remove connection from _in_use and after some time this leads to "Exceeded maximum connections.". I guess changing close_conn from True to False in _connect function should do the trick.
