2017-02-07 5 views
6

私は以下のシナリオを持っていますが、これを達成するために滑らかなDBIOアクションを使用しようとしています。slick dbioアクションのハンドルクリーンアップエラー

上記のシナリオでは、cleanUpアクションを使用しました。しかし、私はmainアクションが失敗した場合にcleanUpアクション結果を返す方法を知らない。

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]です。

スリックバージョン:3.2.0-M2

答えて

0

あなたはそれが最初のアクションと同じ型の値を持つアクションを返すことcleanUpのための署名から見ることができるので、方法はありますが、うではありませんcleanUpアクションから値を返すことができます。

2番目の値を返す場合は、asTryを使用してTryにエラーをラップしてからflatMapを使用する必要があります。ドキュメントはasTryを使用してストリーミング能力を失うことを警告し、あなたがこれを行うための別の方法を見つけたいと思うことがあります...

しかし

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) 
} 

:当面の問題のために、それは次のようになります