-
Notifications
You must be signed in to change notification settings - Fork 686
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
7 changed files
with
92 additions
and
89 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
1 change: 1 addition & 0 deletions
1
exposed/src/main/kotlin/org/jetbrains/exposed/sql/Transaction.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
130 changes: 69 additions & 61 deletions
130
exposed/src/main/kotlin/org/jetbrains/exposed/sql/transactions/experimental/Suspended.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 |
---|---|---|
@@ -1,99 +1,107 @@ | ||
package org.jetbrains.exposed.sql.transactions.experimental | ||
|
||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.selects.SelectClause1 | ||
import kotlinx.coroutines.selects.SelectInstance | ||
import kotlinx.coroutines.sync.withLock | ||
import org.jetbrains.exposed.sql.* | ||
import org.jetbrains.exposed.sql.transactions.* | ||
import org.jetbrains.exposed.sql.transactions.rollbackLoggingException | ||
import java.lang.Exception | ||
import java.util.* | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.coroutineContext | ||
|
||
suspend fun <T> suspendedTransaction(context: CoroutineContext? = null, db: Database? = null, statement: suspend Transaction.() -> T): T { | ||
return suspendedTransactionAsync(context, db, statement = statement).await() | ||
} | ||
internal class TransactionCoroutineElement(val outerTransaction: Transaction?, val newTransaction: Transaction, val manager: TransactionManager) : ThreadContextElement<Transaction> /*by original*/ { | ||
override val key: CoroutineContext.Key<TransactionCoroutineElement> = Companion | ||
|
||
suspend fun <T> Transaction.suspendedTransaction(context: CoroutineContext? = null, statement: suspend Transaction.() -> T): T { | ||
val suspendedTransactionAsyncInternal = suspendedTransactionAsyncInternal(context, db, currentTransaction = this, statement = statement) | ||
return suspendedTransactionAsyncInternal.await() | ||
} | ||
private var prevManager : TransactionManager? = null | ||
|
||
class TransactionResult<T>(internal val transaction: Transaction, | ||
internal val deferred: Deferred<T>, | ||
private val selectClause: SelectClause1<T>, | ||
internal var closeTransaction : Boolean = true) : Deferred<T> by deferred, SelectClause1<T> by selectClause { | ||
override fun updateThreadContext(context: CoroutineContext): Transaction { | ||
prevManager = TransactionManager.currentThreadManager.get() | ||
(manager as? ThreadLocalTransactionManager)?.let { | ||
it.threadLocal.set(newTransaction) | ||
TransactionManager.currentThreadManager.set(manager) | ||
} | ||
return newTransaction | ||
} | ||
|
||
override fun invokeOnCompletion(handler: CompletionHandler): DisposableHandle { | ||
if (closeTransaction) { | ||
try { | ||
transaction.commit() | ||
transaction.closeLoggingException { exposedLogger.warn("Transaction close failed: ${it.message}. Statement: ${transaction.currentStatement}", it) } | ||
} catch (e: Exception) { | ||
transaction.rollbackLoggingException { exposedLogger.warn("Transaction rollback failed: ${it.message}. Statement: ${transaction.currentStatement}", it) } | ||
throw e | ||
override fun restoreThreadContext(context: CoroutineContext, oldState: Transaction) { | ||
require(newTransaction == oldState) | ||
if (outerTransaction == null) { | ||
with(newTransaction) { | ||
try { | ||
commit() | ||
try { | ||
currentStatement?.let { | ||
it.close() | ||
currentStatement = null | ||
} | ||
closeExecutedStatements() | ||
} catch (e: Exception) { | ||
exposedLogger.warn("Statements close failed", e) | ||
} | ||
closeLoggingException { exposedLogger.warn("Transaction close failed: ${it.message}. Statement: $currentStatement", it) } | ||
} catch (e: Exception) { | ||
rollbackLoggingException { exposedLogger.warn("Transaction rollback failed: ${it.message}. Statement: $currentStatement", it) } | ||
throw e | ||
} | ||
} | ||
(manager as? ThreadLocalTransactionManager)?.threadLocal?.remove() | ||
|
||
} else { | ||
(manager as? ThreadLocalTransactionManager)?.threadLocal?.set(outerTransaction) | ||
} | ||
prevManager?.let { | ||
TransactionManager.currentThreadManager.set(it) | ||
} ?: TransactionManager.currentThreadManager.remove() | ||
} | ||
|
||
companion object : CoroutineContext.Key<TransactionCoroutineElement> { | ||
fun forTLManager(manager: TransactionManager, currentTransaction: Transaction?, newTransaction: Transaction) : ThreadContextElement<Transaction> { | ||
return TransactionCoroutineElement(currentTransaction, newTransaction, manager) | ||
} | ||
return deferred.invokeOnCompletion(handler) | ||
} | ||
} | ||
|
||
suspend fun <T> suspendedTransaction(context: CoroutineDispatcher? = null, db: Database? = null, statement: suspend Transaction.() -> T): T { | ||
return suspendedTransactionAsyncInternal(context, db, TransactionManager.currentOrNull(), false, statement).await() | ||
} | ||
|
||
suspend fun <T> Transaction.runSuspended(context: CoroutineDispatcher? = null, statement: suspend Transaction.() -> T): T { | ||
val tx = this | ||
return suspendedTransactionAsyncInternal(context, db, tx, false, statement = statement).await() | ||
} | ||
|
||
class TransactionResult<T>(internal val transaction: Transaction, | ||
internal val deferred: Deferred<T>) : Deferred<T> by deferred | ||
|
||
suspend fun <T, R> TransactionResult<T>.andThen(statement: suspend Transaction.(T) -> R) : TransactionResult<R> { | ||
val currentAsync = this | ||
return suspendedTransactionAsyncInternal(currentTransaction = transaction) { | ||
currentAsync.closeTransaction = false | ||
return suspendedTransactionAsyncInternal(currentTransaction = currentAsync.transaction, lazy = false) { | ||
statement(currentAsync.deferred.await()) | ||
} | ||
} | ||
|
||
suspend fun <T> suspendedTransactionAsync(context: CoroutineContext? = null, db: Database? = null, | ||
suspend fun <T> suspendedTransactionAsync(context: CoroutineDispatcher? = null, db: Database? = null, | ||
useOuterTransactionIfAccessible: Boolean = true, statement: suspend Transaction.() -> T) : TransactionResult<T> { | ||
val currentTransaction = TransactionManager.currentOrNull().takeIf { useOuterTransactionIfAccessible } | ||
return suspendedTransactionAsyncInternal(context, db, currentTransaction, statement) | ||
return suspendedTransactionAsyncInternal(context, db, currentTransaction, false, statement) | ||
} | ||
|
||
private suspend fun <T> suspendedTransactionAsyncInternal(context: CoroutineContext? = null, db: Database? = null, | ||
currentTransaction: Transaction?, statement: suspend Transaction.() -> T) : TransactionResult<T> = coroutineScope { | ||
private suspend fun <T> suspendedTransactionAsyncInternal(context: CoroutineDispatcher? = null, db: Database? = null, | ||
currentTransaction: Transaction?, lazy: Boolean, | ||
statement: suspend Transaction.() -> T) : TransactionResult<T> { | ||
val manager = (currentTransaction?.db ?: db)?.let { TransactionManager.managerFor(it) } | ||
?: TransactionManager.manager | ||
val threadLocalManager = manager as? ThreadLocalTransactionManager | ||
|
||
if (currentTransaction != null) { | ||
val scope = (if (threadLocalManager != null) { | ||
(context ?: coroutineContext) + threadLocalManager.threadLocal.asContextElement(currentTransaction) | ||
} else (context ?: coroutineContext)) + TransactionManager.currentThreadManager.asContextElement(manager) | ||
val asyncLazy = asyncTxInvocation(scope, currentTransaction, statement) | ||
TransactionResult(currentTransaction, asyncLazy, asyncLazy as SelectClause1<T>, false) | ||
} else { | ||
val tx = manager.newTransaction(manager.defaultIsolationLevel) | ||
val scope = (if (threadLocalManager != null) { | ||
(context ?: coroutineContext) + threadLocalManager.threadLocal.asContextElement(tx) | ||
} else (context ?: coroutineContext)) + TransactionManager.currentThreadManager.asContextElement() | ||
val asyncLazy = asyncTxInvocation(scope, tx, statement) | ||
TransactionResult(tx, asyncLazy, asyncLazy as SelectClause1<T>, false) | ||
} | ||
} | ||
|
||
private fun <T> CoroutineScope.asyncTxInvocation(scope: CoroutineContext, tx: Transaction, statement: suspend Transaction.() -> T): Deferred<T> { | ||
return async(context = scope, start = CoroutineStart.LAZY) { | ||
tx.suspendedMutex.withLock { | ||
val tx = currentTransaction ?: manager.newTransaction(manager.defaultIsolationLevel) | ||
val element = TransactionCoroutineElement.forTLManager(manager, currentTransaction, tx) | ||
val contextNew = (context ?: coroutineContext) + element | ||
return withContext(contextNew) { | ||
TransactionResult(tx, async { | ||
try { | ||
tx.statement() | ||
} catch (e: Throwable) { | ||
tx.rollbackLoggingException { exposedLogger.warn("Transaction rollback failed: ${it.message}. Statement: ${tx.currentStatement}", it) } | ||
throw e | ||
} finally { | ||
try { | ||
tx.currentStatement?.let { | ||
it.close() | ||
tx.currentStatement = null | ||
} | ||
tx.closeExecutedStatements() | ||
} catch (e: Exception) { | ||
exposedLogger.warn("Statements close failed", e) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
4 changes: 0 additions & 4 deletions
4
exposed/src/test/kotlin/org/jetbrains/exposed/sql/tests/h2/H2Tests.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
10 changes: 0 additions & 10 deletions
10
exposed/src/test/kotlin/org/jetbrains/exposed/sql/tests/mysql/MysqlTests.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
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