0

implicit val session: DBSession范围内,具体为scalikejdbc.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.

里面的session匹配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.connection因为null.

我怎样才能强制它成为 localTx 而不是 readOnly?

4

1 回答 1

2

通常,AutoSession作为DDL的自动命令会话和插入/Update/Delete OPS的行为,而它作为选择查询的仅阅读会话。

似乎这样做是直截了当的方式。

DB.localTx { implicit session =>
  // Have both the update operation and select query inside this block
}
于 2019-03-18T01:24:12.950 回答