# How to Rollback in JetBrains/Exposed?

**URL:** <https://discuss.kotlinlang.org/t/how-to-rollback-in-jetbrains-exposed/2072>\
**Category:** Libraries\
**Created:** [October 31, 2016, 7:29am UTC](https://discuss.kotlinlang.org/t/how-to-rollback-in-jetbrains-exposed/2072 "2016-10-31T07:29:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnene](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dnene/32/1853_2.png) [@dnene](https://discuss.kotlinlang.org/u/dnene)\
**Post date:** [October 31, 2016, 7:29am UTC](https://discuss.kotlinlang.org/t/how-to-rollback-in-jetbrains-exposed/2072/1 "2016-10-31T07:29:29Z")

</div>

I have started using Kotlin Exposed. One of the early difficulties I got into was that I can’t seem to be able to do a rollback. eg.

transaction {  
val result = doSomething()  
if (result == false) {  
this.rollback()  
}  
}

In the above code, doSomething() invokes many DAO.new {… }. I want to commit or rollback based on the overall result. However all the DAOs automatically get inserted into the table and the transaction committed. If the question is not entirely clear I could easily contribute a test case.

PS: I tried this.connection.rollback() also.

---

<div class="post-metadata">

**Author:** ![douglarek](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/douglarek/32/2088_2.png) [@douglarek](https://discuss.kotlinlang.org/u/douglarek)\
**Post date:** [October 31, 2016, 8:08am UTC](https://discuss.kotlinlang.org/t/how-to-rollback-in-jetbrains-exposed/2072/2 "2016-10-31T08:08:40Z")

</div>

Why not use transaction block in your DAOs ?

---

<div class="post-metadata">

**Author:** ![douglarek](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/douglarek/32/2088_2.png) [@douglarek](https://discuss.kotlinlang.org/u/douglarek)\
**Post date:** [October 31, 2016, 8:10am UTC](https://discuss.kotlinlang.org/t/how-to-rollback-in-jetbrains-exposed/2072/3 "2016-10-31T08:10:09Z")

</div>

A real example in Spring Boot:

```
open fun sendMessage(sender: String, receiver: String, content: Map<String, Any>): Message? {
    return transaction {
        try {
            val message = Chat.slice(Chat.id, Chat.createdAt, Chat.sender, Chat.receiver, Chat.content, Chat.readAt).select {
                Chat.id.eq(Chat.insert {
                    it[Chat.sender] = sender
                    it[Chat.receiver] = receiver
                    it[Chat.content] = js.writeValueAsString(content)
                }[Chat.id]) and Chat.deletedAt.isNull()
            }.limit(1).map {
                Message(id = it[Chat.id], createdAt = it[Chat.createdAt], sender = it[Chat.sender], receiver = it[Chat.receiver], content = it[Chat.content], readAt = it[Chat.readAt])
            }.firstOrNull()

            message?.let { zsetOps.add(sender, receiver, message.id.toDouble()) }
            message
        } catch (e: Exception) {
            rollback()
            throw e
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![dnene](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dnene/32/1853_2.png) [@dnene](https://discuss.kotlinlang.org/u/dnene)\
**Post date:** [November 2, 2016, 5:58am UTC](https://discuss.kotlinlang.org/t/how-to-rollback-in-jetbrains-exposed/2072/4 "2016-11-02T05:58:48Z")

</div>

Thanks for the replies. Finally manged through explicit control of transactions rather than using transaction blocks, since throwing in exception in order to trigger a rollback wasn’t quite appropriate in our context.

---

<div class="post-metadata">

**Author:** ![Tapac](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tapac/32/2193_2.png) [@Tapac](https://discuss.kotlinlang.org/u/Tapac)\
**Post date:** [November 3, 2016, 7:45am UTC](https://discuss.kotlinlang.org/t/how-to-rollback-in-jetbrains-exposed/2072/5 "2016-11-03T07:45:45Z")

</div>

Please try `TransactionManager.current().rollback()`. You can call it anywhere inside `transaction{}` block to roll back all uncommited changes.

---

<div class="post-metadata">

**Author:** ![dnene](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dnene/32/1853_2.png) [@dnene](https://discuss.kotlinlang.org/u/dnene)\
**Post date:** [November 7, 2016, 6:27am UTC](https://discuss.kotlinlang.org/t/how-to-rollback-in-jetbrains-exposed/2072/6 "2016-11-07T06:27:59Z")

</div>

AFAIK, I had tried it (just like I had tried this.rollback()). But nevertheless go back and check once again.

---

<div class="post-metadata">

**Author:** ![dnene](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dnene/32/1853_2.png) [@dnene](https://discuss.kotlinlang.org/u/dnene)\
**Post date:** [November 7, 2016, 12:07pm UTC](https://discuss.kotlinlang.org/t/how-to-rollback-in-jetbrains-exposed/2072/7 "2016-11-07T12:07:53Z")

</div>

I just tried it out. Yes, TransactionManager.current().rollback() worked and rolled back all uncommitted changes.

Thanks
