Scala 事务在失败情况下如何工作
在下面的方法中,当一个事务失败时,如何回滚所有事务? 我能在一个事务中写入所有insert语句吗Scala 事务在失败情况下如何工作,scala,playframework,transactions,rollback,Scala,Playframework,Transactions,Rollback,在下面的方法中,当一个事务失败时,如何回滚所有事务? 我能在一个事务中写入所有insert语句吗 def save(types: List[admin] ): Unit = { try { DB.withTransaction { implicit c => val update = SQL("insert IGNORE into table1 (user_id, full_name, user_name) values ({user_id},{fu
def save(types: List[admin] ): Unit = {
try {
DB.withTransaction { implicit c =>
val update = SQL("insert IGNORE into table1 (user_id, full_name, user_name) values ({user_id},{full_name},{user_name})")
val batch = (update.asBatch /: types)(
(sql, _type) => sql.addBatchParams(_type.user.id, _type.user.name, _type.user.login_id))
batch.execute
}
DB.withTransaction { implicit c =>
val update1 = SQL("INSERT IGNORE INTO table2 (user_id, role_id, is_enabled) values ({user_id},{role_id},{is_enabled})")
val batch1 = (update1.asBatch /: types)(
(sql, _type) => sql.addBatchParams(_type.user.id, _type.role_id, 1))
batch1.execute
}
DB.withTransaction { implicit c =>
val update2 = SQL("INSERT IGNORE INTO table3 (user_id, bu_id, role_id, is_enabled) values ({user_id},{bu_id},{role_id},{is_enabled})")
val batch2 = (update2.asBatch /: types)(
(sql, _type) => sql.addBatchParams(_type.user.id, 1, _type.role_id, 1))
batch2.execute
}
} catch {
case ex: Exception =>
Logger.error("Exception : " + ex.getMessage)
play.Logger.info("Exception" + ex.getMessage)
}
}
是的,您可以将它们全部放在一个事务中。
DB
对象来自Play,而不是来自Anorm本身,后者不管理连接/事务。