Skip to content

Commit e6fdcb5

Browse files
fix bug #90
1 parent 1980769 commit e6fdcb5

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ buildscript {
1717

1818
allprojects {
1919
group = "me.liuwj.ktorm"
20-
version = "2.6"
20+
version = "2.6.1"
2121
}
2222

2323
subprojects { project ->

ktorm-core/src/main/kotlin/me/liuwj/ktorm/database/Database.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,24 @@ class Database(
179179
val current = transactionManager.currentTransaction
180180
val isOuter = current == null
181181
val transaction = current ?: transactionManager.newTransaction(isolation)
182+
var throwable: Throwable? = null
182183

183184
try {
184-
val result = func(transaction)
185-
if (isOuter) transaction.commit()
186-
return result
185+
return func(transaction)
187186
} catch (e: SQLException) {
188-
if (isOuter) transaction.rollback()
189-
throw exceptionTranslator?.invoke(e) ?: e
187+
throwable = exceptionTranslator?.invoke(e) ?: e
188+
throw throwable
190189
} catch (e: Throwable) {
191-
if (isOuter) transaction.rollback()
192-
throw e
190+
throwable = e
191+
throw throwable
193192
} finally {
194-
if (isOuter) transaction.close()
193+
if (isOuter) {
194+
try {
195+
if (throwable == null) transaction.commit() else transaction.rollback()
196+
} finally {
197+
transaction.close()
198+
}
199+
}
195200
}
196201
}
197202

ktorm-support-postgresql/src/test/kotlin/me/liuwj/ktorm/support/postgresql/PostgreSqlTest.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package me.liuwj.ktorm.support.postgresql
22

33
import me.liuwj.ktorm.BaseTest
44
import me.liuwj.ktorm.database.Database
5-
import me.liuwj.ktorm.dsl.eq
6-
import me.liuwj.ktorm.dsl.plus
7-
import me.liuwj.ktorm.dsl.update
5+
import me.liuwj.ktorm.database.useTransaction
6+
import me.liuwj.ktorm.dsl.*
87
import me.liuwj.ktorm.entity.*
98
import me.liuwj.ktorm.logging.ConsoleLogger
109
import me.liuwj.ktorm.logging.LogLevel
@@ -102,4 +101,19 @@ class PostgreSqlTest : BaseTest() {
102101
assert(Employees.findById(1)!!.salary == 1000L)
103102
assert(Employees.findById(5)!!.salary == 1000L)
104103
}
104+
105+
@Test
106+
fun testReturnInTransactionBlock() {
107+
insertTransactional()
108+
assert(Departments.count() == 3)
109+
}
110+
111+
private fun insertTransactional(): Int {
112+
useTransaction {
113+
return Departments.insert {
114+
it.name to "dept name"
115+
it.location to "dept location"
116+
}
117+
}
118+
}
105119
}

0 commit comments

Comments
 (0)