/
ThreadLocalTransactionManager.kt
322 lines (290 loc) · 12.2 KB
/
ThreadLocalTransactionManager.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package org.jetbrains.exposed.sql.transactions
import org.jetbrains.annotations.TestOnly
import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.SqlLogger
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.exposedLogger
import org.jetbrains.exposed.sql.statements.api.ExposedConnection
import org.jetbrains.exposed.sql.statements.api.ExposedSavepoint
import java.sql.SQLException
import java.util.concurrent.ThreadLocalRandom
class ThreadLocalTransactionManager(
private val db: Database,
private val setupTxConnection: ((ExposedConnection<*>, TransactionInterface) -> Unit)? = null
) : TransactionManager {
@Volatile
override var defaultRepetitionAttempts: Int = db.config.defaultRepetitionAttempts
@Deprecated("Use DatabaseConfig to define the defaultRepetitionAttempts")
@TestOnly
set
@Volatile
override var defaultMinRepetitionDelay: Long = db.config.defaultMinRepetitionDelay
@Deprecated("Use DatabaseConfig to define the defaultMinRepetitionDelay")
@TestOnly
set
@Volatile
override var defaultMaxRepetitionDelay: Long = db.config.defaultMaxRepetitionDelay
@Deprecated("Use DatabaseConfig to define the defaultMaxRepetitionDelay")
@TestOnly
set
@Volatile
override var defaultIsolationLevel: Int = db.config.defaultIsolationLevel
get() {
if (field == -1) {
field = Database.getDefaultIsolationLevel(db)
}
return field
}
@Deprecated("Use DatabaseConfig to define the defaultIsolationLevel")
@TestOnly
set
@Volatile
override var defaultReadOnly: Boolean = db.config.defaultReadOnly
val threadLocal = ThreadLocal<Transaction>()
override fun newTransaction(isolation: Int, readOnly: Boolean, outerTransaction: Transaction?): Transaction {
val transaction = outerTransaction?.takeIf { !db.useNestedTransactions } ?: Transaction(
ThreadLocalTransaction(
db = db,
readOnly = outerTransaction?.readOnly ?: readOnly,
transactionIsolation = outerTransaction?.transactionIsolation ?: isolation,
setupTxConnection = setupTxConnection,
threadLocal = threadLocal,
outerTransaction = outerTransaction
)
)
return transaction.apply { bindTransactionToThread(this) }
}
override fun currentOrNull(): Transaction? = threadLocal.get()
override fun bindTransactionToThread(transaction: Transaction?) {
if (transaction != null) {
threadLocal.set(transaction)
} else {
threadLocal.remove()
}
}
private class ThreadLocalTransaction(
override val db: Database,
private val setupTxConnection: ((ExposedConnection<*>, TransactionInterface) -> Unit)?,
override val transactionIsolation: Int,
override val readOnly: Boolean,
val threadLocal: ThreadLocal<Transaction>,
override val outerTransaction: Transaction?
) : TransactionInterface {
private val connectionLazy = lazy(LazyThreadSafetyMode.NONE) {
outerTransaction?.connection ?: db.connector().apply {
setupTxConnection?.invoke(this, this@ThreadLocalTransaction) ?: run {
// The order of setters here is important.
// Transaction isolation should go first as the readOnly or autoCommit can start transaction with wrong isolation level
// Some drivers start a transaction right after `setAutoCommit(false)`,
// which makes `setReadOnly` throw an exception if it is called after `setAutoCommit`
transactionIsolation = this@ThreadLocalTransaction.transactionIsolation
readOnly = this@ThreadLocalTransaction.readOnly
autoCommit = false
}
}
}
override val connection: ExposedConnection<*>
get() = connectionLazy.value
private val useSavePoints = outerTransaction != null && db.useNestedTransactions
private var savepoint: ExposedSavepoint? = if (useSavePoints) connection.setSavepoint(savepointName) else null
override fun commit() {
if (connectionLazy.isInitialized()) {
if (!useSavePoints) {
connection.commit()
} else {
// do nothing in nested. close() will commit everything and release savepoint.
}
}
}
override fun rollback() {
if (connectionLazy.isInitialized() && !connection.isClosed) {
if (useSavePoints && savepoint != null) {
connection.rollback(savepoint!!)
savepoint = connection.setSavepoint(savepointName)
} else {
connection.rollback()
}
}
}
override fun close() {
try {
if (!useSavePoints) {
if (connectionLazy.isInitialized()) connection.close()
} else {
savepoint?.let {
connection.releaseSavepoint(it)
savepoint = null
}
}
} finally {
threadLocal.set(outerTransaction)
}
}
private val savepointName: String
get() {
var nestedLevel = 0
var currenTransaction = outerTransaction
while (currenTransaction?.outerTransaction != null) {
nestedLevel++
currenTransaction = currenTransaction.outerTransaction
}
return "Exposed_savepoint_$nestedLevel"
}
}
}
fun <T> transaction(db: Database? = null, statement: Transaction.() -> T): T =
transaction(
db.transactionManager.defaultIsolationLevel,
db.transactionManager.defaultReadOnly,
db,
statement
)
fun <T> transaction(
transactionIsolation: Int,
readOnly: Boolean = false,
db: Database? = null,
statement: Transaction.() -> T
): T =
keepAndRestoreTransactionRefAfterRun(db) {
val outer = TransactionManager.currentOrNull()
if (outer != null && (db == null || outer.db == db)) {
val outerManager = outer.db.transactionManager
val transaction = outerManager.newTransaction(transactionIsolation, readOnly, outer)
try {
transaction.statement().also {
if (outer.db.useNestedTransactions) {
transaction.commit()
}
}
} finally {
TransactionManager.resetCurrent(outerManager)
}
} else {
val existingForDb = db?.transactionManager
existingForDb?.currentOrNull()?.let { transaction ->
val currentManager = outer?.db.transactionManager
try {
TransactionManager.resetCurrent(existingForDb)
transaction.statement().also {
if (db.useNestedTransactions) {
transaction.commit()
}
}
} finally {
TransactionManager.resetCurrent(currentManager)
}
} ?: inTopLevelTransaction(
transactionIsolation,
readOnly,
db,
null,
statement
)
}
}
fun <T> inTopLevelTransaction(
transactionIsolation: Int,
readOnly: Boolean = false,
db: Database? = null,
outerTransaction: Transaction? = null,
statement: Transaction.() -> T
): T {
fun run(): T {
var repetitions = 0
val outerManager = outerTransaction?.db.transactionManager.takeIf { it.currentOrNull() != null }
var intermediateDelay: Long = 0
var retryInterval: Long? = null
while (true) {
db?.let { db.transactionManager.let { m -> TransactionManager.resetCurrent(m) } }
val transaction = db.transactionManager.newTransaction(transactionIsolation, readOnly, outerTransaction)
@Suppress("TooGenericExceptionCaught")
try {
transaction.db.config.defaultSchema?.let { SchemaUtils.setSchema(it) }
val answer = transaction.statement()
transaction.commit()
return answer
} catch (cause: SQLException) {
handleSQLException(cause, transaction, repetitions)
repetitions++
if (repetitions >= transaction.repetitionAttempts) {
throw cause
}
if (retryInterval == null) {
retryInterval = transaction.getRetryInterval()
intermediateDelay = transaction.minRepetitionDelay
}
// set delay value with an exponential backoff time period.
val delay = when {
transaction.minRepetitionDelay < transaction.maxRepetitionDelay -> {
intermediateDelay += retryInterval * repetitions
ThreadLocalRandom.current().nextLong(intermediateDelay, intermediateDelay + retryInterval)
}
transaction.minRepetitionDelay == transaction.maxRepetitionDelay -> transaction.minRepetitionDelay
else -> 0
}
exposedLogger.warn("Wait $delay milliseconds before retrying")
try {
Thread.sleep(delay)
} catch (cause: InterruptedException) {
// Do nothing
}
} catch (cause: Throwable) {
val currentStatement = transaction.currentStatement
transaction.rollbackLoggingException {
exposedLogger.warn(
"Transaction rollback failed: ${it.message}. Statement: $currentStatement",
it
)
}
throw cause
} finally {
TransactionManager.resetCurrent(outerManager)
closeStatementsAndConnection(transaction)
}
}
}
return keepAndRestoreTransactionRefAfterRun(db) {
run()
}
}
private fun <T> keepAndRestoreTransactionRefAfterRun(db: Database? = null, block: () -> T): T {
val manager = db.transactionManager
val currentTransaction = manager.currentOrNull()
return try {
block()
} finally {
manager.bindTransactionToThread(currentTransaction)
}
}
internal fun handleSQLException(cause: SQLException, transaction: Transaction, repetitions: Int) {
val exposedSQLException = cause as? ExposedSQLException
val queriesToLog = exposedSQLException?.causedByQueries()?.joinToString(";\n") ?: "${transaction.currentStatement}"
val message = "Transaction attempt #$repetitions failed: ${cause.message}. Statement(s): $queriesToLog"
exposedSQLException?.contexts?.forEach {
transaction.interceptors.filterIsInstance<SqlLogger>().forEach { logger ->
logger.log(it, transaction)
}
}
exposedLogger.warn(message, cause)
transaction.rollbackLoggingException {
exposedLogger.warn("Transaction rollback failed: ${it.message}. See previous log line for statement", it)
}
}
internal fun closeStatementsAndConnection(transaction: Transaction) {
val currentStatement = transaction.currentStatement
@Suppress("TooGenericExceptionCaught")
try {
currentStatement?.let {
it.closeIfPossible()
transaction.currentStatement = null
}
transaction.closeExecutedStatements()
} catch (cause: Exception) {
exposedLogger.warn("Statements close failed", cause)
}
transaction.closeLoggingException {
exposedLogger.warn("Transaction close failed: ${it.message}. Statement: $currentStatement", it)
}
}