Skip to content

Commit 3fac8a8

Browse files
committed
sync docs changes to md
1 parent 1408577 commit 3fac8a8

12 files changed

Lines changed: 88 additions & 104 deletions

File tree

docs/api/index.html

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
</li>
6969
<li class="toctree-l3"><a class="reference internal" href="#create_session">create_session</a>
7070
</li>
71+
</ul>
72+
</li>
73+
<li class="toctree-l2"><a class="reference internal" href="#creates-a-new-session-used-internally-by-the-library-you-may-never-need-to-call-it-directly">Creates a new session. Used internally by the library. You may never need to call it directly.</a>
74+
<ul>
7175
<li class="toctree-l3"><a class="reference internal" href="#session_maker">session_maker</a>
7276
</li>
7377
<li class="toctree-l3"><a class="reference internal" href="#close">close</a>
@@ -213,8 +217,7 @@ <h3 id="connect">connect</h3>
213217
</code></pre>
214218
<p>Establishes a connection to the specified host.
215219
It doesn’t need to be called explicitly.
216-
The first session request automatically
217-
establishes the connection.</p>
220+
The first session request automatically establishes the connection.</p>
218221
<hr />
219222
<h3 id="change_host">change_host</h3>
220223
<pre><code class="language-python">async def change_host(self: DBConnect, host: str) -&gt; None:
@@ -225,9 +228,7 @@ <h3 id="change_host">change_host</h3>
225228
<h3 id="create_session">create_session</h3>
226229
<pre><code class="language-python">async def create_session(self: DBConnect) -&gt; AsyncSession:
227230
</code></pre>
228-
<p>Creates a new session the library uses internally.
229-
You never need to call it directly. (Only maybe in some special cases.)</p>
230-
<hr />
231+
<h2 id="creates-a-new-session-used-internally-by-the-library-you-may-never-need-to-call-it-directly">Creates a new session. Used internally by the library. You may never need to call it directly.</h2>
231232
<h3 id="session_maker">session_maker</h3>
232233
<pre><code class="language-python">async def session_maker(self: DBConnect) -&gt; async_sessionmaker[AsyncSession]:
233234
</code></pre>
@@ -304,7 +305,8 @@ <h3 id="atomic_db_session">atomic_db_session</h3>
304305
</code></pre>
305306
<p>A context manager you can use to wrap another function which
306307
uses a context session, making that call isolated within its own transaction.</p>
307-
<p>Several options define how a function handles an open transaction.</p>
308+
<p>There are several options that define how the function will handle
309+
an open transaction.</p>
308310
<p>current_transaction:</p>
309311
<ul>
310312
<li><code>commit</code> - commits the open transaction and starts a new one</li>
@@ -321,15 +323,14 @@ <h3 id="commit_db_session">commit_db_session</h3>
321323
<h3 id="rollback_db_session">rollback_db_session</h3>
322324
<pre><code class="language-python">async def rollback_db_session(connect: DBConnect) -&gt; None:
323325
</code></pre>
324-
<p>Rolls back an active session.</p>
326+
<p>Rollbacks the active session.</p>
325327
<hr />
326328
<h3 id="close_db_session">close_db_session</h3>
327329
<pre><code class="language-python">async def close_db_session(connect: DBConnect) -&gt; None:
328330
</code></pre>
329-
<p>Closes the current context session and returns the connection to the pool.
330-
If you close an uncommitted transaction, the connection rolls back</p>
331-
<p>This is useful when you need to run a database query at the start of the
332-
handler, then continue working over time without keeping the connection open.</p>
331+
<p>Closes the current context session. The connection is returned to the pool.
332+
If you close an uncommitted transaction, the connection rolls back.</p>
333+
<p>Use if you have more work you need to complete without keeping the connection open.</p>
333334
<hr />
334335
<h3 id="new_non_ctx_session">new_non_ctx_session</h3>
335336
<pre><code class="language-python">@asynccontextmanager
@@ -404,7 +405,6 @@ <h3 id="put_savepoint_session_in_ctx">put_savepoint_session_in_ctx</h3>
404405
<p>Sets the context to a session that uses a save point instead of creating
405406
a transaction. You need to pass the session you're using inside
406407
your tests to attach a new session to the same connection.</p>
407-
408408
<pre><code>It is important to use this function inside set_test_context.
409409
</code></pre>
410410
<p>Learn more about <a href="../testing/">testing</a></p>

docs/examples/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ <h3 id="manually-close-the-transaction-and-session">Manually close the transacti
223223

224224
# We closed the transaction
225225
await commit_db_session(connection)
226+
226227
# We closed the session, which returned the connection to the pool automatically.
227228
# Use if you have more work you need to complete without keeping the connection open.
228229
await close_db_session(connection)

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,5 @@ <h2 id="how-it-works">How it works</h2>
221221

222222
<!--
223223
MkDocs version : 1.6.1
224-
Build Date UTC : 2026-01-09 21:16:59.924431+00:00
224+
Build Date UTC : 2026-01-12 21:26:27.089860+00:00
225225
-->

docs/master_replica/index.html

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,24 @@
103103
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
104104
<div class="section" itemprop="articleBody">
105105

106-
<h1 id="masterreplica-or-several-databases-at-the-same-time">Using multiple databases at the same time (master/replica)</h1>
107-
<p><code>db_session</code> and other functions accept a <code>DBConnect</code> instance as input. This approach allows you to work with multiple hosts simultaneously. This system allows you to manage scalability and version control.</p>
108-
<ul>
109-
<li><b>Master:</b> The primary database which runs the "live" version of your information, the location of your primary data set. The master database
110-
handles INSERT, DELETE, and UPDATE API calls.
111-
</li>
112-
<li><b>Replicas:</b> Secondary database(s) which run READ-ONLY versions of your information and receive updates from the master database.</li>
113-
</ul>
106+
<h1 id="using-multiple-database-hosts-masterreplica">Using multiple database hosts (master/replica)</h1>
107+
<p><code>db_session</code> and other functions accept a <code>DBConnect</code> instance as
108+
input.
109+
This approach allows you to work with multiple hosts simultaneously.
110+
This system allows you to manage scalability and reliability.</p>
111+
<ul>
112+
<li><strong>Master</strong> (also called <strong>Primary</strong>): The main database host that handles all write operations (INSERT, UPDATE, DELETE) and read operations. It is the single source of truth</li>
113+
<li><strong>Replica</strong> (also called <strong>Standby</strong> or <strong>Secondary</strong>): Secondary database host(s) which run READ-ONLY versions of your information and receive updates from the master database.</li>
114+
</ul>
114115
<p><code>DBConnect</code> accepts factory functions instead of ready-made objects, making it easy to switch hosts when needed.</p>
115-
116116
<p><code>libpq</code> can detect the master and replica when creating an engine,
117117
but it only does this once - at creation time.
118118
The <code>before_create_session_handler</code> hook allows you to change the host at
119119
runtime if the master or replica changes.
120-
121-
<p><b>You need third-party functionality to determine which host is the master or the replica.</b></p>
122-
<h4 id="change_host">I have an extremely lightweight microservice <a href="https://github.com/krylosov-aa/pg-status">pg-status</a> that fits perfectly here</h4>
123-
<p>The engine is not created when <code>DBConnect</code> initializes, also known as lazy initialization. It is created only on the first request.
120+
You need third-party functionality to determine which host is the master or the replica.</p>
121+
<h4 id="i-have-an-extremely-lightweight-microservice-pg-status-that-fits-perfectly-here">I have an extremely lightweight microservice <a href="https://github.com/krylosov-aa/pg-status">pg-status</a> that fits perfectly here.</h4>
122+
<p>The engine is not created when <code>DBConnect</code> initializes, also known as lazy initialization.
123+
It is created only on the first request.
124124
The library uses lazy initialization in many places.</p>
125125
<pre><code class="language-python">from context_async_sqlalchemy import DBConnect
126126

docs/search/search_index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

docs/sitemap.xml.gz

0 Bytes
Binary file not shown.

docs/testing/index.html

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,30 +106,36 @@
106106
<div class="section" itemprop="articleBody">
107107

108108
<h1 id="testing">Testing</h1>
109-
<p>When testing with a real database, one important problem needs to be solved: ensuring data isolation between tests. There are two approaches:
110-
<ol>
111-
<li><b>Separate sessions</b>: The test uses its own session that prepares data and verifies results after execution. The application also has its own session.
112-
<ul>
113-
<li>It is a more "honest" way to test the application.</li>
114-
<li>Verifies how it handles sessions and transactions automatically.</li>
115-
<li>The ability to inspect the database state when the test is paused.</li>
116-
<li>Complex session management scenarios make other test methods difficult or impossible (e.g., concurrent query execution).</li>
117-
</ul>
118-
<li><b>Shared session and transaction:</b> The test and the application share the same session and transaction.
119-
<ul>
120-
<li>Rolling back transactions is faster and takes less time than starting a new session.</li>
121-
</ul>
122-
</ol>
123-
</p>
124-
109+
<p>When testing with a real database, one important problem needs to be solved: ensuring data isolation between tests.
110+
There are two approaches:</p>
111+
<ol>
112+
<li>
113+
<p><strong>Separate sessions</strong>: The test uses its own session that prepares data and
114+
verifies results after execution. The application also has its own session.</p>
115+
</li>
116+
<li>
117+
<p>It is a more "honest" way to test the application.</p>
118+
</li>
119+
<li>Verifies how it handles sessions and transactions automatically.</li>
120+
<li>
121+
<p>The ability to inspect the database state when the test is paused.
122+
Complex session management scenarios make other test methods difficult or impossible (e.g., concurrent query execution).</p>
123+
</li>
124+
<li>
125+
<p><strong>Shared session and transaction</strong>: The test and the application share the same session and transaction.</p>
126+
</li>
127+
<li>
128+
<p>Rolling back transactions is faster and takes less time than starting a new session.</p>
129+
</li>
130+
</ol>
125131
<p>In my projects, I use both approaches at the same time:</p>
126132
<ul>
127133
<li>For most tests with simple or common logic, I use a shared transaction for the test and the application</li>
128134
<li>For more complex cases, or ones that cannot be tested with separate sessions, I use separate transactions.</li>
129135
</ul>
130-
<p>The library provides several utilities that can be used in tests (e.g., fixtures). They create tests that share a common transaction between the test
131-
and the application. You achieve data isolation by rolling back transactions.
132-
136+
<p>The library provides several utilities that can be used in tests (e.g., fixtures).
137+
They create tests that share a common transaction between the test
138+
and the application. You achieve data isolation by rolling back transactions.</p>
133139
<p>You can see these capabilities in the examples:</p>
134140
<p><a href="https://github.com/krylosov-aa/context-async-sqlalchemy/blob/main/examples/fastapi_example/tests/transactional">Here are tests with a common transaction between the
135141
application and the tests.</a></p>

docs_sources/docs/api.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ async def connect(self: DBConnect, host: str) -> None:
6868
```
6969
Establishes a connection to the specified host.
7070
It doesn’t need to be called explicitly.
71-
If you don't use the call, the first session request will automatically
72-
establishes the connection.
71+
The first session request automatically establishes the connection.
7372

7473
---
7574

@@ -317,7 +316,7 @@ It is intended to test the application when it shares a single transaction.
317316
Use `auto_close=False` if you’re using a test session and transaction
318317
that you close elsewhere in your code.
319318

320-
Use `auto_close=True` if you want to call a function
319+
Use `auto_close=True` to call a function
321320
in a test that uses a context while the middleware is not
322321
active. All sessions close automatically.
323322

docs_sources/docs/concurrent_queries.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
Concurrent query execution deserves special attention.
55
In SQLAlchemy, you can’t run multiple queries concurrently within the same
6-
session - you need to create a new one.
6+
session. You need to create a new one.
77

88
The library provides two simple ways to execute queries concurrently:
99

10-
- Run a function in a new context - `run_in_new_ctx`
11-
- Create a new session that is completely independent of the current context -
10+
- Run a function in a new context: `run_in_new_ctx`
11+
- Create a new session that is independent of the current context:
1212
`new_non_ctx_atomic_session` or `new_non_ctx_session`
1313

1414

docs_sources/docs/examples.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ async def _insert_2() -> None:
121121
# We closed the transaction
122122
await commit_db_session(connection)
123123

124-
# We closed the session and returned the connection to the pool.
124+
# We closed the session, which returned the connection to the pool automatically.
125125
# Use if you have more work you need to complete without keeping the connection open.
126126
await close_db_session(connection)
127127

@@ -227,7 +227,7 @@ async def handler_with_db_session_and_exception() -> None:
227227
await session.execute(stmt)
228228

229229
raise Exception("Some exception")
230-
# transaction rolls back automatically
230+
# transaction automatically rolls back
231231
```
232232

233233
```python

0 commit comments

Comments
 (0)