Tôi có kịch bản dưới đây và tôi đang cố gắng sử dụng các hành động DBIO trơn tru để đạt được nó.Xử lý lỗi hoạt động dọn dẹp trong các hành động dbio trơn tru
Execute a batch Insert operation. On success, return the inserted result On failure,
-> if the failure is due to duplicate value in a particular column, then remove the duplicates from the list, and try batch insert again. If the second batch insert is successful, return a successful future with the second inserted list, else the failed future of the 2nd batch insert.
-> if the failure is due to something else, then throw that exception
Đối với trường hợp trên, tôi đã thử sử dụng tác vụ dọn dẹp. Nhưng, tôi không biết cách trả về kết quả hành động cleanUp nếu hành động chính không thành công.
Làm cách nào để tôi có thể đạt được yêu cầu của mình khi xử lý Lỗi hành động DBIO?
def insertBatchAndReturnQuery(rowList: List[E]): FixedSqlAction[Seq[E], NoStream, Write] = {
query returning query ++= rowList
}
def insert(entities: List[E]): Future[Seq[E]] = {
val q = insertBatchAndReturnQuery(entities).cleanUp {
case Some(ex) => ex match {
case b: PSQLException => {
if (b.getSQLState.equals("23505")) {
//unique key exception, handle this by removing the duplicate entries from the list
???
} else {
throw new Exception("some database exception")
}
}
}
case None => insertBatchAndReturnQuery(Nil)
}
db.run(q)
}
Ở đây, truy vấn là TableQuery [T].
Version Slick: 3.2.0-M2