You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/lifecycle.md
+14-16Lines changed: 14 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
### Bind manager
4
4
5
5
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
7
7
startup and be globally accessible.
8
8
9
9
From SQLAlchemy documentation:
@@ -17,22 +17,20 @@ From SQLAlchemy documentation:
17
17
recommends we create `Session` object at the beginning of a logical operation where
18
18
database access is potentially anticipated.
19
19
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.
22
24
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.
27
27
28
-
The session is not thread safe, therefore the repository is not thread safe as well.
28
+
This means:
29
29
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`)
36
34
37
35
Even using multiple repository instances will work fine, however as they will have completely
38
36
different sessions, it's likely that the second repository will fire additional SELECT queries
@@ -71,8 +69,8 @@ update_my_model()
71
69
```
72
70
///
73
71
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.
76
74
77
75
For example a strategy similar to this would be optimal, if possible:
0 commit comments