Skip to content

Commit 623679b

Browse files
Move connection pooling to its own page in docs (#1315)
* move connection pooling to its own page * tweak docs --------- Co-authored-by: Daniel Townsend <dan@dantownsend.co.uk>
1 parent a0d802c commit 623679b

4 files changed

Lines changed: 85 additions & 94 deletions

File tree

docs/src/piccolo/engines/cockroach_engine.rst

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,55 +27,10 @@ to learn more.
2727

2828
-------------------------------------------------------------------------------
2929

30-
Connection pool
30+
Connection Pool
3131
---------------
3232

33-
To use a connection pool, you need to first initialise it. The best place to do
34-
this is in the startup event handler of whichever web framework you are using.
35-
36-
Here's an example using Starlette. Notice that we also close the connection
37-
pool in the shutdown event handler.
38-
39-
.. code-block:: python
40-
41-
from piccolo.engine import engine_finder
42-
from starlette.applications import Starlette
43-
44-
45-
app = Starlette()
46-
47-
48-
@app.on_event('startup')
49-
async def open_database_connection_pool():
50-
engine = engine_finder()
51-
await engine.start_connection_pool()
52-
53-
54-
@app.on_event('shutdown')
55-
async def close_database_connection_pool():
56-
engine = engine_finder()
57-
await engine.close_connection_pool()
58-
59-
.. hint:: Using a connection pool helps with performance, since connections
60-
are reused instead of being created for each query.
61-
62-
Once a connection pool has been started, the engine will use it for making
63-
queries.
64-
65-
.. hint:: If you're running several instances of an app on the same server,
66-
you may prefer an external connection pooler - like pgbouncer.
67-
68-
Configuration
69-
~~~~~~~~~~~~~
70-
71-
The connection pool uses the same configuration as your engine. You can also
72-
pass in additional parameters, which are passed to the underlying database
73-
adapter. Here's an example:
74-
75-
.. code-block:: python
76-
77-
# To increase the number of connections available:
78-
await engine.start_connection_pool(max_size=20)
33+
See :ref:`ConnectionPool`.
7934

8035
-------------------------------------------------------------------------------
8136

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
.. _ConnectionPool:
2+
3+
Connection Pool
4+
===============
5+
6+
.. hint:: Connection pools can be used with Postgres and CockroachDB.
7+
8+
Setup
9+
~~~~~
10+
11+
To use a connection pool, you need to first initialise it. The best place to do
12+
this is in the startup event handler of whichever web framework you are using.
13+
We also want to close the connection pool in the shutdown event handler.
14+
15+
The recommended way for Starlette and FastAPI apps is to use the ``lifespan``
16+
parameter:
17+
18+
.. code-block:: python
19+
20+
from contextlib import asynccontextmanager
21+
from piccolo.engine import engine_finder
22+
from starlette.applications import Starlette
23+
24+
25+
@asynccontextmanager
26+
async def lifespan(app: Starlette):
27+
engine = engine_finder()
28+
assert engine
29+
await engine.start_connection_pool()
30+
yield
31+
await engine.close_connection_pool()
32+
33+
34+
app = Starlette(lifespan=lifespan)
35+
36+
In older versions of Starlette and FastAPI, you may need event handlers
37+
instead:
38+
39+
.. code-block:: python
40+
41+
from piccolo.engine import engine_finder
42+
from starlette.applications import Starlette
43+
44+
45+
app = Starlette()
46+
47+
48+
@app.on_event('startup')
49+
async def open_database_connection_pool():
50+
engine = engine_finder()
51+
await engine.start_connection_pool()
52+
53+
54+
@app.on_event('shutdown')
55+
async def close_database_connection_pool():
56+
engine = engine_finder()
57+
await engine.close_connection_pool()
58+
59+
.. hint:: Using a connection pool helps with performance, since connections
60+
are reused instead of being created for each query.
61+
62+
Once a connection pool has been started, the engine will use it for making
63+
queries.
64+
65+
.. hint:: If you're running several instances of an app on the same server,
66+
you may prefer an external connection pooler - like pgbouncer.
67+
68+
-------------------------------------------------------------------------------
69+
70+
Configuration
71+
~~~~~~~~~~~~~
72+
73+
The connection pool uses the same configuration as your engine. You can also
74+
pass in additional parameters, which are passed to the underlying database
75+
adapter. Here's an example:
76+
77+
.. code-block:: python
78+
79+
# To increase the number of connections available:
80+
await engine.start_connection_pool(max_size=20)

docs/src/piccolo/engines/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,4 @@ Engine types
127127
./sqlite_engine
128128
./postgres_engine
129129
./cockroach_engine
130+
./connection_pool

docs/src/piccolo/engines/postgres_engine.rst

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,10 @@ to learn more.
2626

2727
-------------------------------------------------------------------------------
2828

29-
Connection pool
29+
Connection Pool
3030
---------------
3131

32-
To use a connection pool, you need to first initialise it. The best place to do
33-
this is in the startup event handler of whichever web framework you are using.
34-
35-
Here's an example using Starlette. Notice that we also close the connection
36-
pool in the shutdown event handler.
37-
38-
.. code-block:: python
39-
40-
from piccolo.engine import engine_finder
41-
from starlette.applications import Starlette
42-
43-
44-
app = Starlette()
45-
46-
47-
@app.on_event('startup')
48-
async def open_database_connection_pool():
49-
engine = engine_finder()
50-
await engine.start_connection_pool()
51-
52-
53-
@app.on_event('shutdown')
54-
async def close_database_connection_pool():
55-
engine = engine_finder()
56-
await engine.close_connection_pool()
57-
58-
.. hint:: Using a connection pool helps with performance, since connections
59-
are reused instead of being created for each query.
60-
61-
Once a connection pool has been started, the engine will use it for making
62-
queries.
63-
64-
.. hint:: If you're running several instances of an app on the same server,
65-
you may prefer an external connection pooler - like pgbouncer.
66-
67-
Configuration
68-
~~~~~~~~~~~~~
69-
70-
The connection pool uses the same configuration as your engine. You can also
71-
pass in additional parameters, which are passed to the underlying database
72-
adapter. Here's an example:
73-
74-
.. code-block:: python
75-
76-
# To increase the number of connections available:
77-
await engine.start_connection_pool(max_size=20)
32+
See :ref:`ConnectionPool`.
7833

7934
-------------------------------------------------------------------------------
8035

0 commit comments

Comments
 (0)