Scala 在ScalikeJDBC中更新返回的查询
使用Scala 在ScalikeJDBC中更新返回的查询,scala,scalikejdbc,Scala,Scalikejdbc,使用隐式val会话:范围内的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()
隐式val会话:范围内的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
我尝试创建自己的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)
此操作失败,因为会话。连接为null
如何将其强制为localTx而不是只读?通常,自动会话
充当DDL和insert/update/delete ops的自动提交会话,而它充当select查询的只读会话
它似乎是这样做的,以下是直截了当的方法
DB.localTx { implicit session =>
// Have both the update operation and select query inside this block
}
通常,AutoSession
充当DDL和insert/update/delete ops的自动提交会话,而它充当select查询的只读会话
它似乎是这样做的,以下是直截了当的方法
DB.localTx { implicit session =>
// Have both the update operation and select query inside this block
}
谢谢是的,我更愿意这样做,但我在playframework中除了一个热切绑定的Autosession之外什么都没有。我明白了scalikejdbc.TxBoundary.Future
可能对您的用例有用。我很抱歉。当然,我可以调用DB.localTx
。我不知道为什么我认为我不能谢谢是的,我更愿意这样做,但我在playframework中除了一个热切绑定的Autosession之外什么都没有。我明白了scalikejdbc.TxBoundary.Future
可能对您的用例有用。我很抱歉。当然,我可以调用DB.localTx
。我不知道为什么我认为我不能