المعاملات (Transactions)
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- ACID
- حدود المعاملة
- @Transactional
- قواعد التراجع (Rollback)
تعلّم أولًا
3 ساعة تدريب (تقدير)
مخرجات التعلم
- Explain ACID with concrete failure scenarios.
- Place
@Transactionalat the service layer around a business operation. - Explain default rollback rules (unchecked vs checked) and configure them.
- Explain the proxy mechanism and diagnose self-invocation failures.
- Use read-only transactions and keep transactions short (no remote calls inside).
الدروس
المختبر والتمارين
Lab
JNR-031-LAB1 · Place order
Outcomes: 1–5 · Time: ~3 h
- Implement
OrderService.placeOrderthat reserves stock and creates the order in one transaction. - 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.
- Add
readOnly = trueon the order query method. - Fix the provided money-transfer exercise that has a planted self-invocation bug; add a test that fails first.
- 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)
| ID | Outcome | Task |
|---|---|---|
| JNR-031-EX01 | 1 | Give one concrete failure for each of A, C, I, and D using Shop examples. |
| JNR-031-EX02 | 2 | Move @Transactional from the controller to the correct service method. |
| JNR-031-EX03 | 3 | A method throws a checked PaymentException and data is still committed. Fix it in two ways. |
| JNR-031-EX04 | 4 | Explain in your own words why this.method() skips the transaction, and give two fixes. |
| JNR-031-EX05 | 5 | Restructure placeOrder so the payment API is called outside the transaction. |
| JNR-031-EX06 | 3, 4 | Fix 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
placeOrdergets@Transactional; controllers stay free of it. - EX03
@Transactional(rollbackFor = PaymentException.class)or make the exception extendRuntimeException. - EX04 The proxy intercepts only calls from other beans;
thisis 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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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.