具有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().toIteratororg.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呢?
发布于 2019-03-18 01:24:12
通常,AutoSession作为DDL和insert/update/delete ops的自动提交会话,而作为select查询的只读会话工作。
它似乎是这样做的,以下是直截了当的方式。
DB.localTx { implicit session =>
// Have both the update operation and select query inside this block
}https://stackoverflow.com/questions/55213167
复制相似问题