0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【ktor】エラーが発生した場合のロールバック対応方法

Posted at

はじめに

引き続きサーバー側の対応を進めています。
今回はタイトルにある通り、エラーが発生した場合のロールバック対応を備忘録的に記事にしておこうと思います。

ロールバック対応

対応の詳細は以下です。

internal fun <T> PipelineContext<Unit, ApplicationCall>.rollbackTransaction(statement: Transaction.() -> T): T {
    return transaction {
        try {
            statement()
        }
        catch (ex: Exception) {
            rollback()
            throw ex
        }
    }
}

以上になります。
extensionで実装しておくと呼び出しもこれまでと変わらず対応できて便利ですね。
transactionを返す前にdbに接続するような処理を実装してもいいと思います。

さいごに

最近時間の進行の速さに頭を悩まされています、もう2月中旬とは、、、

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?