更新ScalikeJDBC中的返回查询

时间:2019-03-18 00:02:35

标签: scala scalikejdbc

范围为li:first-child {},具体为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 transactionsession内部匹配,并假定它应该是只读的:

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

我如何将其强制为localTx而不是readOnly?

1 个答案:

答案 0 :(得分:1)

通常,AutoSession充当DDL和插入/更新/删除操作的自动提交会话,而充当选择查询的只读会话。

这似乎是直接的方法。

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