2017-02-07 7 views
6

मेरे पास निम्न परिदृश्य है और मैं इसे प्राप्त करने के लिए slick डीबीआईओ कार्यों का उपयोग करने की कोशिश कर रहा हूं।स्लिक डीबीओ क्रियाओं में क्लीनअप एक्शन त्रुटि को संभालें

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 

उपरोक्त परिदृश्य के लिए, मैंने क्लीनअप कार्रवाई का उपयोग करने का प्रयास किया। लेकिन, मुझे नहीं पता कि मुख्य कार्य विफल होने पर क्लीनअप क्रिया परिणाम कैसे वापस आना है।

डीबीआईओ क्रियाओं का उपयोग कर मेरी आवश्यकता को कैसे प्राप्त कर सकता हूं त्रुटि प्रबंधन?

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 [टी] है।

स्लिक संस्करण: 3.2.0-M2

उत्तर

0

आप cleanUp है कि यह पहली कार्रवाई के रूप में एक ही प्रकार के मूल्य के साथ एक कार्रवाई रिटर्न के लिए हस्ताक्षर से देख सकते हैं, इसलिए वहाँ कोई रास्ता नहीं आप करेंगे क्लीनअप कार्रवाई से मूल्य वापस करने में सक्षम हो।

यदि आप दूसरा मान वापस करना चाहते हैं, तो आपको का उपयोग करके का उपयोग करके अपनी त्रुटि को लपेटने की आवश्यकता है, और फिर 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 का उपयोग कर क्षमता स्ट्रीमिंग खो देता है, तो आप एक और तरीका यह करने के लिए खोजने के लिए चाहते हो सकता है ...

संबंधित मुद्दे