-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from qiaoyuang/main
Add a new concurrency safe API
- Loading branch information
Showing
23 changed files
with
246 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
sample/src/jvmMain/kotlin/com/ctrip/sqllin/sample/DatabasePath.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (C) 2022 Ctrip.com. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.ctrip.sqllin.sample | ||
|
||
import com.ctrip.sqllin.driver.DatabasePath | ||
import com.ctrip.sqllin.driver.toDatabasePath | ||
|
||
actual val databasePath: DatabasePath | ||
get() = System.getProperty("user.dir").toDatabasePath() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# 并发安全 | ||
|
||
在 `1.2.2` 版本之前, _sqllin-dsl_ 无法保证并发安全。如果你想在不同的线程中共享同一个 `Database` | ||
对象,这可能会导致不可预测的结果。所以最佳的方式是:当你想要操作你的数据库时,创建一个 `Database` | ||
对象,当你结束的你的操作时,立即关闭它。 | ||
|
||
但是这非常不方便,我们总是必须频繁地创建数据库连接并关闭它,这是一种对资源的浪费。举例来说, | ||
如果我们正在开发一款 Android app,并且在单个页面中(Activity/Fragment),我们希望我们可以持有一个 | ||
`Database` 对象,当我们想要在后台线程(或协程)中操作数据库时,直接使用它,并在某些生命周期函数内关闭它 | ||
(`onDestroy`、`onStop` 等等)。 | ||
|
||
这种情况下,当我们在不同线程(或协程)中共享 `Database` 对象时,我们应该确保并发安全。所以,从 `1.2.2` | ||
版本开始,我们可以使用新 API `Database#suspendedScope` 来代替旧的 `database {}` 用法。比如说,如果我们有如下旧代码: | ||
|
||
```kotlin | ||
fun sample() { | ||
database { | ||
PersonTable { table -> | ||
table INSERT Person(age = 4, name = "Tom") | ||
table INSERT listOf( | ||
Person(age = 10, name = "Nick"), | ||
Person(age = 3, name = "Jerry"), | ||
Person(age = 8, name = "Jack"), | ||
) | ||
} | ||
} | ||
} | ||
``` | ||
我们使用新 API `Database#suspendedScope` 来代替旧的 `database {}` 后,将会是这样: | ||
|
||
```kotlin | ||
fun sample() { | ||
database suspendScope { | ||
PersonTable { table -> | ||
table INSERT Person(age = 4, name = "Tom") | ||
table INSERT listOf( | ||
Person(age = 10, name = "Nick"), | ||
Person(age = 3, name = "Jerry"), | ||
Person(age = 8, name = "Jack"), | ||
) | ||
} | ||
} | ||
} | ||
``` | ||
`suspendedScope` 是一个挂起函数。在 `suspendedScope` 内部,所有的操作都是原子性的。这意味着:如果你共享了同一个 | ||
`Database` 对象到不同的协程中,它可以保证后执行的 `suspendedScope` 会等待先执行的 `suspendedScope` 执行完成。 | ||
|
||
## 接下来 | ||
|
||
- [SQL 函数](sql-functions-cn.md) | ||
- [高级查询](advanced-query-cn.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Concurrency Safety | ||
|
||
Before the version `1.2.2`, _sqllin-dsl_ can't ensure the concurrency safety. If | ||
you want to share a `Database` object between different threads, that would lead to | ||
unpredictable consequences. So, the best way is when you want to operate your | ||
database, create a `Database` object, and when you finish your operating, close it immediately. | ||
|
||
But, that's very inconvenient, we always have to create a database connection and | ||
close it frequently, that is a waste of resources. For example, if we are developing | ||
an Android app, and in a single page(Activity/Fragment), we hope we can keep a | ||
`Database` object, when we want to operate the database in background threads(or | ||
coroutines), just use it, and, close it in certain lifecycle | ||
functions(`onDestroy`, `onStop`, etc..). | ||
|
||
In that time, we should make sure the concurrency safety that we sharing the `Database` | ||
object between different threads(or coroutines). So, start with the version `1.2.2`, we can | ||
use the new API `Database#suspendedScope` to replace the usage of `database {}`. For | ||
example, if we have some old code: | ||
|
||
```kotlin | ||
fun sample() { | ||
database { | ||
PersonTable { table -> | ||
table INSERT Person(age = 4, name = "Tom") | ||
table INSERT listOf( | ||
Person(age = 10, name = "Nick"), | ||
Person(age = 3, name = "Jerry"), | ||
Person(age = 8, name = "Jack"), | ||
) | ||
} | ||
} | ||
} | ||
``` | ||
We use the new API `Database#suspendedScope` to replace the `database {}`, it will be like that: | ||
|
||
```kotlin | ||
fun sample() { | ||
database suspendScope { | ||
PersonTable { table -> | ||
table INSERT Person(age = 4, name = "Tom") | ||
table INSERT listOf( | ||
Person(age = 10, name = "Nick"), | ||
Person(age = 3, name = "Jerry"), | ||
Person(age = 8, name = "Jack"), | ||
) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The `suspendedScope` is a suspend function. Inside the `suspendedScope`, the all operations are | ||
atomic. That means: If you share the same `Database` object between two coroutines, it can ensure the | ||
`suspendedScope` executing later will wait for the one executing earlier to finish. | ||
|
||
## Next Step | ||
|
||
- [SQL Functions](sql-functions.md) | ||
- [Advanced Query](advanced-query.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.