การย้อนกลับไปยัง savepoint

Transactions and Error Handling in PostgreSQL

Jason Myers

Principal Engineer

แบบไม่มี savepoint

BEGIN TRANSACTION;

UPDATE cost = 2.33 WHERE name = 'Linga';

UPDATE cost = 500 WHERE name = 'Macaron';

ROLLBACK;
Transactions and Error Handling in PostgreSQL

การใช้ savepoint และการย้อนกลับแบบเจาะจง

BEGIN TRANSACTION;

UPDATE cost = 2.33 WHERE name = 'Linga';

SAVEPOINT oops;
UPDATE cost = 500 WHERE name = 'Macaron';
ROLLBACK TO oops;
Transactions and Error Handling in PostgreSQL

การปล่อย savepoint เมื่อใช้งานเสร็จแล้ว

BEGIN TRANSACTION;

UPDATE cost = 2.33 WHERE name = 'Linga';
UPDATE cost = 2.33 WHERE name = 'Petit-Beurre';
UPDATE cost = 2.33 WHERE name = 'Rosette';

SAVEPOINT oops;

UPDATE cost = 5.00 WHERE name = 'Macaron';
UPDATE cost = 3.50 WHERE name = 'Panellets';
RELEASE SAVEPOINT oops;
Transactions and Error Handling in PostgreSQL

สิ่งสำคัญ 2 ประการเกี่ยวกับ rollback และ savepoint

  • ROLLBACK ที่ไม่มี TO จะย้อนกลับทั้ง transaction
  • ROLLBACK TO ___ ที่ ___ ไม่ใช่ชื่อ savepoint ที่ถูกต้องจะเกิดข้อผิดพลาด
Transactions and Error Handling in PostgreSQL

มาฝึกกันเถอะ!

Transactions and Error Handling in PostgreSQL

Preparing Video For Download...