now all transaction in database realisation are working in IO thread

This commit is contained in:
InsanusMokrassar 2019-11-11 15:13:22 +06:00
parent bd750c09a3
commit b4c694aea9
2 changed files with 12 additions and 4 deletions

View File

@ -4,10 +4,12 @@ import com.insanusmokrassar.postssystem.core.content.*
import com.insanusmokrassar.postssystem.core.content.api.ContentAPI import com.insanusmokrassar.postssystem.core.content.api.ContentAPI
import com.insanusmokrassar.postssystem.core.utils.generateContentId import com.insanusmokrassar.postssystem.core.utils.generateContentId
import com.insanusmokrassar.postssystem.core.utils.pagination.* import com.insanusmokrassar.postssystem.core.utils.pagination.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
@ -18,10 +20,12 @@ private class ContentAPIDatabaseTable(
internal val idColumn = text("_id") internal val idColumn = text("_id")
internal val dataColumn = text("data") internal val dataColumn = text("data")
private inline fun <T> transaction(noinline body: Transaction.() -> T): T = database.transaction(body) private suspend inline fun <T> transaction(noinline body: Transaction.() -> T): T = withContext(Dispatchers.IO) {
database.transaction(body)
}
init { init {
transaction { database.transaction {
SchemaUtils.createMissingTablesAndColumns(this@ContentAPIDatabaseTable) SchemaUtils.createMissingTablesAndColumns(this@ContentAPIDatabaseTable)
} }
} }

View File

@ -6,10 +6,12 @@ import com.insanusmokrassar.postssystem.core.post.api.PostsAPI
import com.insanusmokrassar.postssystem.core.utils.generatePostId import com.insanusmokrassar.postssystem.core.utils.generatePostId
import com.insanusmokrassar.postssystem.core.utils.pagination.* import com.insanusmokrassar.postssystem.core.utils.pagination.*
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.withContext
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
private class PostsAPIContentRelations( private class PostsAPIContentRelations(
@ -77,10 +79,12 @@ private class PostsAPIDatabaseTable(
private val postUpdatedBroadcastChannel = BroadcastChannel<RegisteredPost>(Channel.BUFFERED) private val postUpdatedBroadcastChannel = BroadcastChannel<RegisteredPost>(Channel.BUFFERED)
override val postUpdatedFlow: Flow<RegisteredPost> = postUpdatedBroadcastChannel.asFlow() override val postUpdatedFlow: Flow<RegisteredPost> = postUpdatedBroadcastChannel.asFlow()
private inline fun <T> transaction(noinline body: Transaction.() -> T): T = database.transaction(body) private suspend inline fun <T> transaction(noinline body: Transaction.() -> T): T = withContext(Dispatchers.IO) {
database.transaction(body)
}
init { init {
transaction { database.transaction {
SchemaUtils.createMissingTablesAndColumns(this@PostsAPIDatabaseTable) SchemaUtils.createMissingTablesAndColumns(this@PostsAPIDatabaseTable)
} }
} }