Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scalikejdbc.DBConnection#readOnly does not honor isolationLevel #1143

Open
dvoet opened this issue Dec 15, 2020 · 5 comments
Open

scalikejdbc.DBConnection#readOnly does not honor isolationLevel #1143

dvoet opened this issue Dec 15, 2020 · 5 comments
Labels

Comments

@dvoet
Copy link

dvoet commented Dec 15, 2020

NamedDB(dbName.name).isolationLevel(IsolationLevel.Serializable).readOnly does not propagate the isolation level down to the database connection. When using a connection pool, readOnly reuses the isolation level set by the previous use of the connection.

Our workaround sets the isolation directly on the connection:

  def readOnly[A](f: DBSession => A): A = {
    val db = NamedDB(name)
    db.conn.setTransactionIsolation(TRANSACTION_SERIALIZABLE)
    db.readOnly(f)
  }
@dvoet
Copy link
Author

dvoet commented Dec 17, 2020

I also suspect using IsolationLevel.Default inherits the isolation level from the last transaction used by a connection in a pool.

https://github.com/scalikejdbc/scalikejdbc/blob/master/scalikejdbc-core/src/main/scala/scalikejdbc/Tx.scala#L27

@seratch
Copy link
Member

seratch commented Dec 17, 2020

Thanks for sharing this. The recommended way for performing queries with a particular isolation level is to use a localTx block. It works even when you do not run updates in it.

NamedDB(dbName.name).isolationLevel(IsolationLevel.Serializable).localTx { implicit s =>
   // select query here
}

@dvoet
Copy link
Author

dvoet commented Dec 18, 2020 via email

@seratch
Copy link
Member

seratch commented Dec 18, 2020

Thanks for the prompt reply. While I am open to supporting read-only transactions, I am not going to work on it in the short term. Actually, we got a request #542 four years ago but it's not done yet. Please go with your workaround for now.

@dvoet
Copy link
Author

dvoet commented Dec 18, 2020 via email

@seratch seratch added the core label Feb 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants