2017-02-07 37 views
6

Aşağıdaki senaryo var ve bunu başarmak için kaygan DBIO eylemlerini kullanmaya çalışıyorum.Temizleme eylemi hatasını kaygan dbio eylemleriyle temizleme

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 

Yukarıdaki senaryo için, cleanUp eylemini kullanmayı denedim. Ancak, ana eylem başarısız olursa, cleanUp eylemlerinin sonucunu nasıl döndüreceğimi bilmiyorum.

İhtiyaçımı DBIO İşlemleri hatalarını kullanarak nasıl sağlayabilirim? Burada

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

, sorgu TableQuery [T] 'dir.

Slick Versiyon: 3.2.0-M2

cevap

0

Bunu ilk eylem olarak aynı tipte değerine sahip bir eylem döndüren cleanUp için imza görebileceğiniz, böylece hiçbir şekilde orada olacak olan değeri cleanUp eyleminden döndürmek.

Eğer ikinci değeri döndürmek isterseniz, hatalarınızı asTry kullanarak sarmanız ve ardından flatMap kullanın. ...

dokümantasyon asTry kullanarak kapasitesini akışı kaybeder konusunda uyarıyor Ancak

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

yüzden bunu yapmak için başka bir yol bulmak isteyebilirsiniz: el sorunu için, böyle bir şey olmazdı