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

المعاملات (Transactions)

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

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

نظرة عامة

الموضوعات

  • ACID
  • حدود المعاملة
  • @Transactional
  • قواعد التراجع (Rollback)

تعلّم أولًا

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

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

  1. Explain ACID with concrete failure scenarios.
  2. Place @Transactional at the service layer around a business operation.
  3. Explain default rollback rules (unchecked vs checked) and configure them.
  4. Explain the proxy mechanism and diagnose self-invocation failures.
  5. Use read-only transactions and keep transactions short (no remote calls inside).

الدروس

  1. 01ACID بالأمثلة
  2. 02حدود المعاملة
  3. 03@Transactional والوكلاء (Proxies)
  4. 04قواعد التراجع (Rollback)
  5. 05أساسيات الانتشار (Propagation)
  6. 06المعاملات الطويلة والاستدعاءات البعيدة
  7. 07عرض عملي: جعل عملية الدفع (Checkout) ذرّية
  8. 08عرض عملي: اكتشاف خطأ الاستدعاء الذاتي (Self-invocation)

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

Lab

JNR-031-LAB1 · Place order

Outcomes: 1–5 · Time: ~3 h

  1. Implement OrderService.placeOrder that reserves stock and creates the order in one transaction.
  2. Write failure tests: out-of-stock on the second line and a forced exception after saving the order; assert no stock change and no order row remains.
  3. Add readOnly = true on the order query method.
  4. Fix the provided money-transfer exercise that has a planted self-invocation bug; add a test that fails first.
  5. Keep any payment call outside the transaction (a stub is enough).

Acceptance: failure tests prove atomicity.

Exercises (assessment: fix a broken transaction implementation and explain why it failed)

IDOutcomeTask
JNR-031-EX011Give one concrete failure for each of A, C, I, and D using Shop examples.
JNR-031-EX022Move @Transactional from the controller to the correct service method.
JNR-031-EX033A method throws a checked PaymentException and data is still committed. Fix it in two ways.
JNR-031-EX044Explain in your own words why this.method() skips the transaction, and give two fixes.
JNR-031-EX055Restructure placeOrder so the payment API is called outside the transaction.
JNR-031-EX063, 4Fix a broken transaction implementation (provided) and write the explanation.
أظهر الحلول
  • EX01 A: crash after debit, before credit. C: stock would become negative. I: another request reads half-updated stock. D: committed order disappears after a restart without durability.
  • EX02 The service method placeOrder gets @Transactional; controllers stay free of it.
  • EX03 @Transactional(rollbackFor = PaymentException.class) or make the exception extend RuntimeException.
  • EX04 The proxy intercepts only calls from other beans; this is the raw object. Fix: move the method to another bean, or put the annotation on the public entry method.
  • EX05 Call payment first (or after commit) in a non-transactional coordinator method; the transactional part only reserves stock and saves the order.
  • EX06 Typical faults: annotation on a private method, a swallowed exception in a catch, or self-invocation; fix and describe which proxy rule was broken.
  • Outcome 1 is also covered by the quiz.

الاختبار

Outcomes: O1 ACID · O2 boundaries at the service layer · O3 rollback rules · O4 proxies and self-invocation · O5 short and read-only transactions.

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

  1. 1Atomicity means:O1
  2. 2Durability means committed data:O1
  3. 3@Transactional usually belongs on:O2
  4. 4The transaction boundary should match:O2
  5. 5By default Spring rolls back on:O3
  6. 6Catching an exception inside the method without rethrowing:O3
  7. 7Self-invocation fails because:O4
  8. 8A reliable fix for self-invocation:O4
  9. 9A payment API call inside a transaction is bad because:O5
  10. 10readOnly = true is meant for:O5

الجزء العملي

Six exercises JNR-031-EX01 to EX06 plus the lab; the assessment is fixing a broken transaction implementation and explaining why it failed (EX06). Pass: the fix works, the explanation names the proxy rule, and the lab failure tests prove atomicity.

أخطاء شائعة

@Transactional on private methods or controllers; calling a payment API inside a transaction; swallowing exceptions so nothing rolls back.

التقييم

Fix a broken transaction implementation and explain why it failed.

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

Junior implementation; isolation and locking in MID-015/016.

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