انتقل إلى المحتوى
أمين أبوزيد
MID-016

المعاملات المتقدمة

  • 7 درس
  • 3 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • القفل المتفائل والمتشائم
  • سلوك إعادة المحاولة
  • انتشار المعاملات (transaction propagation)

تعلّم أولًا

3 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Implement optimistic locking with @Version and handle conflicts as 409 responses.
  2. Choose optimistic vs pessimistic locking for a workload.
  3. Use Spring transaction propagation (REQUIRES_NEW, NESTED) correctly and recognize its traps.
  4. Implement safe retries for transient failures (serialization, deadlock) with idempotent units of work.

الدروس

  1. 01القفل المتفائل
  2. 02القفل المتشائم من JPA
  3. 03اختيار استراتيجية القفل
  4. 04الانتشار (Propagation) بعمق
  5. 05إعادة محاولة المعاملات بأمان
  6. 06عرض عملي: تعديلات متزامنة باستخدام @Version
  7. 07عرض عملي: فخ سجل التدقيق مع REQUIRES_NEW

المختبر والتمارين

Lab

MID-016-LAB1 · Concurrent product edits

Outcomes: 1–4 · Time: ~3 h Add optimistic locking to Product: two admins editing the same product, the second gets HTTP 409 with the current version. Then make a deadlock-prone job (updates two products in opposite orders) retry safely with a limited, idempotent retry. Acceptance: automated tests for both conflicts: the 409 case and the deadlock-then-retry case.

Exercises (assessment: fix a transaction design)

IDOutcomeTask
MID-016-EX011Add @Version to an entity and show the SQL of an update with the version check.
MID-016-EX021Map the optimistic lock exception to a 409 response with the current version.
MID-016-EX032Choose optimistic, pessimistic, or atomic SQL for: profile edit, stock decrement, seat booking, nightly price update. Justify each.
MID-016-EX043Predict and verify what happens when a @Transactional method calls another method of the same class with REQUIRES_NEW.
MID-016-EX053With pool size 2, show a pool deadlock from REQUIRES_NEW and fix it.
MID-016-EX064Review a retry loop that retries a payment call and a DB write on any exception; list the problems and correct it.
أظهر الحلول
  • EX01 update product set name=?, version=? where id=? and version=?; version increments on success.
  • EX02 @ExceptionHandler(ObjectOptimisticLockingFailureException.class) returns 409 and re-reads the product for the body.
  • EX03 Profile edit: optimistic. Stock decrement: atomic UPDATE. Seat booking: pessimistic or unique constraint. Nightly price update: batch with optimistic check or lock by chunk (reasons vary; check justification).
  • EX04 No new transaction starts: self-invocation bypasses the Spring proxy; move to another bean.
  • EX05 Each request holds a connection and waits for another; with all connections taken, all wait until timeout. Fix: avoid nesting, enlarge pool only as a temporary measure, or write after commit.
  • EX06 Retry only transient exceptions, with a limit and backoff; payment needs an idempotency key; retry the whole transaction outside the transactional method.
  • Common mistakes (non-idempotent retries, pool exhaustion) are in EX05 and EX06.

الاختبار

Outcomes: O1 optimistic locking and 409 · O2 choosing a strategy · O3 propagation · O4 safe retries.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1@Version makes Hibernate:O1
  2. 2A failed version check should usually return HTTP:O1
  3. 3For optimistic locking across two HTTP requests the client must:O1
  4. 4Best fit for optimistic locking:O2
  5. 5Best first choice for a hot stock decrement:O2
  6. 6REQUIRES_NEW runs the method in:O3
  7. 7NESTED propagation is implemented with:O3
  8. 8Calling a @Transactional(REQUIRES_NEW) method from the same class:O3
  9. 9Which errors are safe to retry?O4
  10. 10Retried units of work should be:O4

الجزء العملي

Fix a given transaction design (a service with a missing version check, a self-invocation bug, and an unsafe retry). Pass: quiz 8/10, all three defects found and fixed, and the lab tests passing.

أخطاء شائعة

Retrying non-idempotent work; REQUIRES_NEW exhausting the connection pool.

التقييم

Fix a transaction design.

الصلة بالمسار الوظيفي

Distributed consistency (MID-030).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.