File tree Expand file tree Collapse file tree 3 files changed +31
-12
lines changed
ktorm-core/src/main/kotlin/me/liuwj/ktorm/database
ktorm-support-postgresql/src/test/kotlin/me/liuwj/ktorm/support/postgresql Expand file tree Collapse file tree 3 files changed +31
-12
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ buildscript {
1717
1818allprojects {
1919 group = " me.liuwj.ktorm"
20- version = " 2.6"
20+ version = " 2.6.1 "
2121}
2222
2323subprojects { project ->
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -2,9 +2,8 @@ package me.liuwj.ktorm.support.postgresql
22
33import me.liuwj.ktorm.BaseTest
44import 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.*
87import me.liuwj.ktorm.entity.*
98import me.liuwj.ktorm.logging.ConsoleLogger
109import 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}
You can’t perform that action at this time.
0 commit comments