2010-09-16 21 views
5

संभाल नेट इकाई की रूपरेखा का मूल्यांकन मैं आशावादी संगामिति मोड के साथ समवर्ती अपडेट प्रबंधित करने की सही पैटर्न खोजने की कोशिश।इकाई की रूपरेखा OptimisticConcurrencyException

documentation और अन्य कई स्थानों मैं निम्नलिखित पैटर्न को देखने में:

 

Try 
    ' Try to save changes, which may cause a conflict. 
    Dim num As Integer = context.SaveChanges() 
    Console.WriteLine("No conflicts. " & num.ToString() & " updates saved.") 
Catch generatedExceptionName As OptimisticConcurrencyException 
    ' Resolve the concurrency conflict by refreshing the 
    ' object context before re-saving changes. 
    context.Refresh(RefreshMode.ClientWins, orders) 

    ' Save changes. 
    context.SaveChanges() 
    Console.WriteLine("OptimisticConcurrencyException handled and changes saved") 
End Try 

मैं इस

  • के साथ निम्न समस्याओं को देखने यह स्वचालित रूप से लागू करता है पिछले-इन-जीत के बजाय आशावादी का उपयोग करने का मोड
  • यह मजबूत नहीं है: .refresh और .SaveChanges के बीच समवर्ती परिवर्तन एक नया OptimisticConcurrencyException पैदा कर सकता है
  • 012,351,

यह सही है, या मैं कुछ याद आ रही है?

एक UI में मैं सामान्य रूप से उपयोगकर्ता संगामिति संघर्ष का समाधान करते हैं:

 

Try 
    _ctx.SaveChanges() 
Catch ex As OptimisticConcurrencyException 
    MessageBox.Show("Data was modified by another User." & vbCrLf & 
    "Click 'Refresh' to show the current values and reapply your changes.", 
    "Concurrency Violation", MessageBoxButton.OK) 
End Try 

व्यापार तर्क में मैं सामान्य रूप से पूरे व्यापार लेनदेन के चारों ओर एक पुन: प्रयास करें पाश का उपयोग करें (पढ़ने और अद्यतन):

 

Const maxRetries = 5, retryDelayMs = 500 
For i = 1 To maxRetries 
    Try 
     Using ctx As New EFConcurrencyTest.ConcurrencyTestEntities 
      ctx.Inventories.First.QuantityInStock += 1 
      System.Threading.Thread.Sleep(3000) 'Cause conflict 
      ctx.SaveChanges() 
     End Using 
     Exit For 
    Catch ex As OptimisticConcurrencyException 
     If i = maxRetries Then Throw 
     System.Threading.Thread.Sleep(retryDelayMs) 
    End Try 
Next 

डब्ल्यू ith एफई मैं पाश संपुटित करने की योजना:

 

ExecuteOptimisticSubmitChanges(Of EFConcurrencyTest.ConcurrencyTestEntities)(
    Sub(ctx) 
     ctx.Inventories.First.QuantityInStock += 1 
     System.Threading.Thread.Sleep(3000) 'Cause conflict 
    End Sub) 

देखें:

Functional Optimistic Concurrency in C#

Retryable actions in C#

उत्तर

6

यह:

Catch ex As OptimisticConcurrencyException 
    ' Resolve the concurrency conflict by refreshing the 
    ' object context before re-saving changes. 
    context.Refresh(RefreshMode.ClientWins, orders) 

    ' Save changes. 
    context.SaveChanges() 
    Console.WriteLine("OptimisticConcurrencyException handled and changes saved") 

... पूरी तरह से व्यर्थ है। यदि आप एकमात्र चीज करते हैं तो आप अपवाद को "संभाल" करते हैं, इसे अनदेखा करना और वैसे भी सहेजना है, आपको केवल आशावादी समरूपता बंद करनी चाहिए; आप एक वैकल्पिक सुविधा के आसपास काम करने के लिए कोड लिख रहे हैं।

तो, हाँ, मैं कहेंगे प्रलेखन आप अच्छी सलाह यहाँ दे रही है नहीं है।

आपका प्रस्तावित यूआई कोड एक बेहतर समाधान है।

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