https://kotlinlang.org logo
#squarelibraries
Title
# squarelibraries
m

Manuel Pérez Alcolea

03/29/2023, 6:15 AM
is it thread-safe to request multiple transactions from different threads on JVM with sqlite? Using the provided
JdbcSqliteDriver
Copy code
val database: Database = ...
val queries: MyQueries = database.myQueries

// request for transaction in thread A
queries.insertPerson("Joseph")

// request for transaction in thread B
queries.insertPerson("Martina")
a

Andrew Alexander

03/29/2023, 12:48 PM
Generally not with sqlite
e

eygraber

03/29/2023, 2:08 PM
I'm pretty sure it's thread safe
m

Manuel Pérez Alcolea

03/29/2023, 8:54 PM
I'm gonna assume it is because this is part of its implementation.
Copy code
private class ThreadedConnectionManager(
  private val url: String,
  private val properties: Properties,
) : JdbcSqliteDriverConnectionManager() {
  private val transactions = ThreadLocal<Transaction>()
  private val connections = ThreadLocal<Connection>()

  override var transaction: Transaction?
    get() = transactions.get()
    set(value) { transactions.set(value) }

  override fun getConnection() = connections.getOrSet {
    DriverManager.getConnection(url, properties)
  }
if they clean up the `ThreadLocal`s later (notice I'm talking about NEW transactions that stay in the same thread, not a transaction that jumps onto another thread when it's already started. My question was about starting multiple transactions concurrently)
12 Views