-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
902 additions
and
505 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
60 changes: 60 additions & 0 deletions
60
documentation-website/Writerside/topics/Asynchronous-Support.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,60 @@ | ||
# Asynchronous Support | ||
|
||
## Working with Coroutines | ||
|
||
In the modern world, non-blocking and asynchronous code is popular. Kotlin | ||
has [Coroutines](https://kotlinlang.org/docs/reference/coroutines-overview.html) that give you an imperative way of writing asynchronous code. Most | ||
Kotlin frameworks (like [ktor](https://ktor.io)) have built-in support for Coroutines while Exposed is mostly blocking. Why? Because Exposed uses JDBC API to interact | ||
with databases that was designed in an era of blocking APIs. Moreover, Exposed stores some values in | ||
thread-local variables while coroutines could (and will) be executed in different threads. | ||
|
||
Since Exposed 0.15.1, there are bridge functions that will give you a safe way to interact with Exposed within `suspend` | ||
blocks: `newSuspendedTransaction/Transaction.withSuspendTransaction` have the same parameters as a blocking `transaction` function but will allow you to | ||
provide a `CoroutineDispatcher` in which the function will be executed. If context is not provided, your code will be executed within the current `coroutineContext`. | ||
|
||
Sample usage looks like: | ||
|
||
```kotlin | ||
runBlocking { | ||
transaction { | ||
SchemaUtils.create(FooTable) // Table will be created on a current thread | ||
|
||
newSuspendedTransaction(Dispatchers.Default) { | ||
FooTable.insert { it[id] = 1 } // This insert will be executed in one of Default dispatcher threads | ||
|
||
withSuspendTransaction { | ||
val id = FooTable.select { FooTable.id eq 1 } | ||
.single()()[FooTable.id] // This select also will be executed on some thread from Default dispatcher using the same transaction | ||
} | ||
} | ||
|
||
val result = newSuspendedTransaction(Dispatchers.IO) { | ||
FooTable.select { FooTable.id eq 1 } | ||
.single()[H2Tests.Testing.id] // This select will be executed on some thread from IO dispatcher using the same transaction | ||
} | ||
} | ||
} | ||
|
||
``` | ||
|
||
Please note that such code remains blocking (as it still uses JDBC) and you should not try to share a transaction between multiple threads as it will | ||
lead to undefined behaviour. | ||
|
||
If you want to execute some code asynchronously and use the result later in your code, take a look at `suspendedTransactionAsync` function. | ||
|
||
```kotlin | ||
val launchResult = suspendedTransactionAsync(Dispatchers.IO, db = db) { | ||
FooTable.insert {} | ||
|
||
FooTable.select { FooTable.id eq 1 }.singleOrNull()?.getOrNull(Testing.id) | ||
} | ||
|
||
println("Result: " + (launchResult.await() ?: - 1)) | ||
|
||
``` | ||
|
||
This function will accept the same parameters as `newSuspendedTransaction` above, but returns `Deferred` which you could call `await` on to achieve your | ||
result. | ||
|
||
`suspendedTransactionAsync` is always executed in a new transaction to prevent concurrency issues when queries execution order could be changed | ||
by `CoroutineDispatcher`. |
177 changes: 0 additions & 177 deletions
177
documentation-website/Writerside/topics/Database-and-DataSource.md
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
# Databases | ||
|
||
[Working with Database](Working-with-Database.md) | ||
|
||
[Working with DataSource](Working-with-DataSource.md) | ||
|
||
[Working with Transaction](Working-with-Transaction.md) |
2 changes: 1 addition & 1 deletion
2
...tion-website/Writerside/topics/DAO-API.md → ...e/Writerside/topics/Deep-Dive-into-DAO.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
Oops, something went wrong.