【发布时间】:2019-03-18 09:24:12
【问题描述】:
在范围内使用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?
【问题讨论】:
标签: scala scalikejdbc