在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?