Skip to content

Commit 5fa178b

Browse files
change the default isolation to null #231
1 parent 40c704a commit 5fa178b

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

ktorm-core/src/main/kotlin/org/ktorm/database/Database.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,15 @@ public class Database(
313313
* - Any exceptions thrown in the callback function can trigger a rollback.
314314
* - This function is reentrant, so it can be called nested. However, the inner calls don’t open new transactions
315315
* but share the same ones with outers.
316+
* - Since version 3.3.0, the default isolation has changed to null (stands for the default isolation level of the
317+
* underlying datastore), not [TransactionIsolation.REPEATABLE_READ] anymore.
316318
*
317-
* @param isolation transaction isolation, enums defined in [TransactionIsolation].
319+
* @param isolation transaction isolation, null for the default isolation level of the underlying datastore.
318320
* @param func the executed callback function.
319321
* @return the result of the callback function.
320322
*/
321323
@OptIn(ExperimentalContracts::class)
322-
public inline fun <T> useTransaction(
323-
isolation: TransactionIsolation = TransactionIsolation.REPEATABLE_READ,
324-
func: (Transaction) -> T
325-
): T {
324+
public inline fun <T> useTransaction(isolation: TransactionIsolation? = null, func: (Transaction) -> T): T {
326325
contract {
327326
callsInPlace(func, InvocationKind.EXACTLY_ONCE)
328327
}

ktorm-core/src/main/kotlin/org/ktorm/database/JdbcTransactionManager.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ import javax.sql.DataSource
3535
public class JdbcTransactionManager(public val connector: () -> Connection) : TransactionManager {
3636
private val threadLocal = ThreadLocal<Transaction>()
3737

38-
override val defaultIsolation: TransactionIsolation = TransactionIsolation.REPEATABLE_READ
38+
override val defaultIsolation: TransactionIsolation? = null
3939

4040
override val currentTransaction: Transaction? get() = threadLocal.get()
4141

42-
override fun newTransaction(isolation: TransactionIsolation): Transaction {
42+
override fun newTransaction(isolation: TransactionIsolation?): Transaction {
4343
if (currentTransaction != null) {
4444
throw IllegalStateException("Current thread is already in a transaction.")
4545
}
@@ -51,16 +51,18 @@ public class JdbcTransactionManager(public val connector: () -> Connection) : Tr
5151
return connector.invoke()
5252
}
5353

54-
private inner class JdbcTransaction(private val desiredIsolation: TransactionIsolation) : Transaction {
55-
private var originIsolation = defaultIsolation.level
54+
private inner class JdbcTransaction(private val desiredIsolation: TransactionIsolation?) : Transaction {
55+
private var originIsolation = -1
5656
private var originAutoCommit = true
5757

5858
private val connectionLazy = lazy(LazyThreadSafetyMode.NONE) {
5959
newConnection().apply {
6060
try {
61-
originIsolation = transactionIsolation
62-
if (originIsolation != desiredIsolation.level) {
63-
transactionIsolation = desiredIsolation.level
61+
if (desiredIsolation != null) {
62+
originIsolation = transactionIsolation
63+
if (originIsolation != desiredIsolation.level) {
64+
transactionIsolation = desiredIsolation.level
65+
}
6466
}
6567

6668
originAutoCommit = autoCommit
@@ -83,7 +85,7 @@ public class JdbcTransactionManager(public val connector: () -> Connection) : Tr
8385
}
8486

8587
override fun rollback() {
86-
if (connectionLazy.isInitialized() && !connection.isClosed) {
88+
if (connectionLazy.isInitialized()) {
8789
connection.rollback()
8890
}
8991
}
@@ -101,7 +103,7 @@ public class JdbcTransactionManager(public val connector: () -> Connection) : Tr
101103
@Suppress("SwallowedException")
102104
private fun Connection.closeSilently() {
103105
try {
104-
if (originIsolation != desiredIsolation.level) {
106+
if (desiredIsolation != null && originIsolation != desiredIsolation.level) {
105107
transactionIsolation = originIsolation
106108
}
107109
if (originAutoCommit) {

ktorm-core/src/main/kotlin/org/ktorm/database/SpringManagedTransactionManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public class SpringManagedTransactionManager(public val dataSource: DataSource)
3535

3636
private val proxy = dataSource as? TransactionAwareDataSourceProxy ?: TransactionAwareDataSourceProxy(dataSource)
3737

38-
override val defaultIsolation: TransactionIsolation get() = TransactionIsolation.REPEATABLE_READ
38+
override val defaultIsolation: TransactionIsolation? = null
3939

4040
override val currentTransaction: Transaction? = null
4141

42-
override fun newTransaction(isolation: TransactionIsolation): Nothing {
42+
override fun newTransaction(isolation: TransactionIsolation?): Nothing {
4343
val msg = "Transaction is managed by Spring, please use Spring's @Transactional annotation instead."
4444
throw UnsupportedOperationException(msg)
4545
}

ktorm-core/src/main/kotlin/org/ktorm/database/TransactionManager.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ import java.sql.Connection
3030
public interface TransactionManager {
3131

3232
/**
33-
* The default transaction isolation.
33+
* The default transaction isolation, null for the default isolation level of the underlying datastore.
3434
*/
35-
public val defaultIsolation: TransactionIsolation
35+
public val defaultIsolation: TransactionIsolation?
3636

3737
/**
3838
* The opened transaction of the current thread, null if there is no transaction opened.
@@ -46,7 +46,7 @@ public interface TransactionManager {
4646
* @return the new-created transaction.
4747
* @throws [IllegalStateException] if there is already a transaction opened.
4848
*/
49-
public fun newTransaction(isolation: TransactionIsolation = defaultIsolation): Transaction
49+
public fun newTransaction(isolation: TransactionIsolation? = defaultIsolation): Transaction
5050

5151
/**
5252
* Create a native JDBC connection to the database.

ktorm-global/src/main/kotlin/org/ktorm/global/Global.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,16 @@ public inline fun <T> useConnection(func: (Connection) -> T): T {
247247
* - Any exceptions thrown in the callback function can trigger a rollback.
248248
* - This function is reentrant, so it can be called nested. However, the inner calls don’t open new transactions
249249
* but share the same ones with outers.
250+
* - Since version 3.3.0, the default isolation has changed to null (stands for the default isolation level of the
251+
* underlying datastore), not [TransactionIsolation.REPEATABLE_READ] anymore.
250252
*
251-
* @param isolation transaction isolation, enums defined in [TransactionIsolation].
253+
* @param isolation transaction isolation, null for the default isolation level of the underlying datastore.
252254
* @param func the executed callback function.
253255
* @return the result of the callback function.
254256
* @see Database.useTransaction
255257
*/
256258
@OptIn(ExperimentalContracts::class)
257-
public inline fun <T> useTransaction(
258-
isolation: TransactionIsolation = TransactionIsolation.REPEATABLE_READ,
259-
func: (Transaction) -> T
260-
): T {
259+
public inline fun <T> useTransaction(isolation: TransactionIsolation?, func: (Transaction) -> T): T {
261260
contract {
262261
callsInPlace(func, InvocationKind.EXACTLY_ONCE)
263262
}

0 commit comments

Comments
 (0)