在下面的方法中,如果一个失败,我怎么能回滚所有交易? 我能在单个事务中编写所有插入语句吗?
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)
}
}