16
16
17
17
package fr .acinq .eclair .db .pg
18
18
19
+ import com .zaxxer .hikari .util .IsolationLevel
19
20
import fr .acinq .eclair .db .Monitoring .Metrics ._
20
21
import fr .acinq .eclair .db .Monitoring .Tags
21
22
import fr .acinq .eclair .db .jdbc .JdbcUtils
22
23
import fr .acinq .eclair .db .pg .PgUtils .PgLock .LockFailureHandler .LockException
23
24
import grizzled .slf4j .Logging
24
25
import org .postgresql .util .{PGInterval , PSQLException }
25
26
26
- import java .sql .{Connection , Statement , Timestamp }
27
+ import java .sql .{Connection , Timestamp }
27
28
import java .util .UUID
28
29
import javax .sql .DataSource
29
30
import scala .concurrent .duration ._
@@ -242,11 +243,14 @@ object PgUtils extends JdbcUtils {
242
243
243
244
}
244
245
245
- def inTransaction [T ](connection : Connection )(f : Connection => T ): T = {
246
- val autoCommit = connection.getAutoCommit
246
+ /**
247
+ * @param isolationLevel Be careful when changing the default value
248
+ */
249
+ private def inTransactionInternal [T ](isolationLevel : IsolationLevel )(connection : Connection )(f : Connection => T ): T = {
250
+ val previousAutoCommit = connection.getAutoCommit
247
251
connection.setAutoCommit(false )
248
- val isolationLevel = connection.getTransactionIsolation
249
- connection.setTransactionIsolation(Connection . TRANSACTION_SERIALIZABLE )
252
+ val previousIsolationLevel = connection.getTransactionIsolation
253
+ connection.setTransactionIsolation(isolationLevel.getLevelId )
250
254
try {
251
255
val res = f(connection)
252
256
connection.commit()
@@ -256,14 +260,20 @@ object PgUtils extends JdbcUtils {
256
260
connection.rollback()
257
261
throw ex
258
262
} finally {
259
- connection.setAutoCommit(autoCommit )
260
- connection.setTransactionIsolation(isolationLevel )
263
+ connection.setAutoCommit(previousAutoCommit )
264
+ connection.setTransactionIsolation(previousIsolationLevel )
261
265
}
262
266
}
263
267
264
268
def inTransaction [T ](f : Connection => T )(implicit dataSource : DataSource ): T = {
265
269
withConnection { connection =>
266
- inTransaction(connection)(f)
270
+ inTransactionInternal(IsolationLevel .TRANSACTION_SERIALIZABLE )(connection)(f)
271
+ }
272
+ }
273
+
274
+ def inTransaction [T ](isolationLevel : IsolationLevel )(f : Connection => T )(implicit dataSource : DataSource ): T = {
275
+ withConnection { connection =>
276
+ inTransactionInternal(isolationLevel)(connection)(f)
267
277
}
268
278
}
269
279
0 commit comments