|
1 | 1 | # Concurrency Safety |
2 | 2 |
|
3 | 3 | Before the version `1.2.2`, _sqllin-dsl_ can't ensure the concurrency safety. If |
4 | | -you want to share a `Database` object between different threads, that would lead to |
| 4 | +you want to share a `Database` instance between different threads, that would lead to |
5 | 5 | unpredictable consequences. So, the best way is when you want to operate your |
6 | | -database, create a `Database` object, and when you finish your operating, close it immediately. |
| 6 | +database, create a `Database` instance, and when you finish your operating, close it immediately. |
7 | 7 |
|
8 | 8 | But, that's very inconvenient, we always have to create a database connection and |
9 | 9 | close it frequently, this is a waste of resources. For example, if we are developing |
10 | 10 | an Android app, and in a single page (Activity/Fragment), we hope we can keep a |
11 | | -`Database` object, when we want to operate the database in background threads (or |
| 11 | +`Database` instance, when we want to operate the database in background threads (or |
12 | 12 | coroutines), just use it, and, close it in certain lifecycle |
13 | 13 | functions (`onDestroy`, `onStop`, etc.). |
14 | 14 |
|
15 | 15 | At that time, we should make sure the concurrency safety that we share the `Database` |
16 | | -object between different threads (or coroutines). So, start with the version `1.2.2`, we can |
| 16 | +instance between different threads (or coroutines). So, start with the version `1.2.2`, we can |
17 | 17 | use the new API `Database#suspendedScope` to replace the usage of `database {}`. For |
18 | 18 | example, if we have some old code: |
19 | 19 |
|
@@ -49,7 +49,7 @@ fun sample() { |
49 | 49 | ``` |
50 | 50 |
|
51 | 51 | The `suspendedScope` is a suspend function. Inside the `suspendedScope`, the all operations are |
52 | | -atomic. That means: If you share the same `Database` object between two coroutines, it can ensure the |
| 52 | +atomic. That means: If you share the same `Database` instance between two coroutines, it can ensure the |
53 | 53 | `suspendedScope` executing later will wait for the one executing earlier to finish. |
54 | 54 |
|
55 | 55 | ## Next Step |
|
0 commit comments