Skip to content

Commit 2fe3098

Browse files
committed
Update lifecycle documentation
1 parent 3d3d68e commit 2fe3098

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

docs/lifecycle.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
### Bind manager
44

55
The `SQLAlchemyBindManager` object holds all the SQLAlchemy Engines, which
6-
are supposed to be global objects. Therefore it should be created on application
6+
are supposed to be global objects, therefore it should be created on application
77
startup and be globally accessible.
88

99
From SQLAlchemy documentation:
@@ -17,22 +17,20 @@ From SQLAlchemy documentation:
1717
recommends we create `Session` object at the beginning of a logical operation where
1818
database access is potentially anticipated.
1919

20-
The repository starts a `Session` for each _operation_, in order to maintain isolation.
21-
This means you can create a repository object almost whenever you want.
20+
The repository is not built for parallel execution, it keeps a `Session` object scoped to
21+
its lifecycle to avoid unnecessary queries, and executes a transaction for each _operation_
22+
to maintain isolation. This means you can create a repository object almost whenever you want,
23+
as long as you don't run parallel operations.
2224

23-
/// details | There two exceptions: creating repositories in global variables or concurrent asyncio tasks
24-
type: warning
25-
The repository creates and maintain the lifecycle of a session object to avoid
26-
emitting unnecessary queries to refresh the model state on new session.
25+
The session in the repository is not thread safe and [is not safe on concurrent asyncio tasks](https://docs.sqlalchemy.org/en/20/orm/session_basics.html#is-the-session-thread-safe-is-asyncsession-safe-to-share-in-concurrent-tasks)
26+
therefore the repository has the same limitations.
2727

28-
The session is not thread safe, therefore the repository is not thread safe as well.
28+
This means:
2929

30-
Check the [Notes on multithreaded applications](/manager/session/#note-on-multithreaded-applications)
31-
32-
The `AsyncSession` [is not safe on concurrent asyncio tasks](https://docs.sqlalchemy.org/en/20/orm/session_basics.html#is-the-session-thread-safe-is-asyncsession-safe-to-share-in-concurrent-tasks),
33-
therefore the same repository instance can't be used in multiple asyncio tasks like
34-
when using `asyncio.gather()`
35-
///
30+
* Do not assign a repository object to a global variable
31+
(Check the [Notes on multithreaded applications](/manager/session/#note-on-multithreaded-applications))
32+
* Do not share a repository instance in multiple threads or processes (e.g. using `asyncio.to_thread`).
33+
* Do not use the same repository in concurrent asyncio task (e.g. using `asyncio.gather`)
3634

3735
Even using multiple repository instances will work fine, however as they will have completely
3836
different sessions, it's likely that the second repository will fire additional SELECT queries
@@ -71,8 +69,8 @@ update_my_model()
7169
```
7270
///
7371

74-
The recommendation is of course to use the same repository instance, where possible,
75-
and structure your code in a way to match the single repository instance approach.
72+
The recommendation is of course to try to write your application in a way you cna use
73+
a single repository instance, where possible.
7674

7775
For example a strategy similar to this would be optimal, if possible:
7876

0 commit comments

Comments
 (0)