Skip to content

Commit

Permalink
Cancel transactions on all kind of exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
rorbech committed Jan 9, 2024
1 parent ce3d333 commit e8145ac
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Fixed
* Using keypaths in Flows could sometimes throw `java.lang.IllegalStateException: [RLM_ERR_WRONG_THREAD]: Realm accessed from incorrect thread.`. (Issue [#1594](https://github.com/realm/realm-kotlin/pull/1594, since 1.13.0)
* Non-`IllegalStateExceptions` in a `write`-block would not cancel transactions, but leave it open. (Issue [#1615](https://github.com/realm/realm-kotlin/issues/1615)).

### Compatibility
* File format: Generates Realms with file format v23.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal class SuspendableWriter(
if (shouldClose.value)
throw IllegalStateException("Cannot commit transaction on closed realm")
}
} catch (e: IllegalStateException) {
} catch (e: Throwable) {
if (realm.isInTransaction()) {
realm.cancelWrite()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class RealmTests {

@Suppress("invisible_member")
@Test
fun exceptionInWriteWillRollback() = runBlocking {
fun exceptionInWriteWillRollback() = runBlocking<Unit> {
class CustomException : Exception()

assertFailsWith<CustomException> {
Expand All @@ -189,6 +189,11 @@ class RealmTests {
}
}
assertEquals(0, realm.query<Child>().find().size)
// Verify that we can do additional transactions after the previous transaction
// was rolled back due to the exception
realm.write {
this.copyToRealm(Child())
}
}

@Test
Expand Down

0 comments on commit e8145ac

Please sign in to comment.