用一个
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?