Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 处理slick dbio操作中的清理操作错误_Scala_Exception Handling_Slick - Fatal编程技术网

Scala 处理slick dbio操作中的清理操作错误

Scala 处理slick dbio操作中的清理操作错误,scala,exception-handling,slick,Scala,Exception Handling,Slick,我有下面的场景,我正在尝试使用巧妙的DBIO操作来实现它 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

我有下面的场景,我正在尝试使用巧妙的DBIO操作来实现它

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
对于上述场景,我尝试使用清理操作。但是,如果主操作失败,我不知道如何返回清理操作结果

如何使用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)
  }
这里的查询是TableQuery[T]


Slick版本:3.2.0-M2

您可以从
清理
的签名中看到,它返回的操作的值与第一个操作的值类型相同,因此无法从清理操作返回值

如果希望返回第二个值,则需要使用
asTry
将错误包装在
Try
中,然后使用
flatMap
。对于手头的问题,它看起来是这样的:

val q = insertBatchAndReturnQuery(entities).asTry.flatMap {
  case Failure(b: PSQLException) if b.getSQLState == "23505" =>
    //unique key exception, handle this 
    //by removing the duplicate entries from the list
    ???
  case Failure(e) =>
    throw new Exception("some database exception")
  case Success(count) => DBIO.successful(count)
}

但是,文档警告说,使用asTry会丢失流媒体容量,因此您可能希望找到另一种方法来实现这一点。

您可以从
清理
的签名中看到,它返回的操作值与第一个操作的值类型相同,因此,无法从清理操作返回值

如果希望返回第二个值,则需要使用
asTry
将错误包装在
Try
中,然后使用
flatMap
。对于手头的问题,它看起来是这样的:

val q = insertBatchAndReturnQuery(entities).asTry.flatMap {
  case Failure(b: PSQLException) if b.getSQLState == "23505" =>
    //unique key exception, handle this 
    //by removing the duplicate entries from the list
    ???
  case Failure(e) =>
    throw new Exception("some database exception")
  case Success(count) => DBIO.successful(count)
}
然而,文档警告说,使用asTry会丢失流媒体容量,因此您可能希望找到另一种方法来实现这一点