簡體   English   中英

更新ScalikeJDBC中的返回查詢

[英]Update returning queries in ScalikeJDBC

使用implicit val session: DBSession域中的scalikejdbc.AutoSessionscalikejdbc.AutoSession

更新工作

sql"""
    update payments set status=${status.name} where id in ($ids)
""".update().apply()

並選擇工作

sql"""
   select id
   from payments
   where status='valid'
""".map(_.long).list().apply()

但是更新返回列失敗,因為事務被設置為只讀。

sql"""
  update payments
  set status='submitted'
  where status='pending'
  and scheduled <= ${ZonedDateTime.now.toInstant}
  returning id
""".map(_.long).iterable().apply().toIterator

org.postgresql.util.PSQLException: ERROR: cannot execute UPDATE in a read-only transaction

sessionSQLToResult內部SQLToResult ,並假定它應該是只讀的:

  case AutoSession | ReadOnlyAutoSession => DB.readOnly(f)

我嘗試創建自己的DBSession以避免與該模式匹配,但是放棄了該方法。 我最接近使其工作的是:

val writeableSession: DBSession = DBSession(session.connection, isReadOnly = false)

def inner()(implicit session: DBSession): Iterator[Payment] = {
  sql"""
  update payments
  set status='submitted'
  where status='pending'
  returning id
""".map(_.long).iterable().apply().toIterator
}

inner()(writeableSession)

這失敗,因為session.connectionnull

我怎樣才能強迫它成為localTx而不是readOnly?

通常, AutoSession充當DDL和insert / update / delete ops的自動提交會話,而它充當select查詢的只讀會話。

這樣做似乎很簡單。

DB.localTx { implicit session =>
  // Have both the update operation and select query inside this block
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM