README.savepoints 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. SQL Language Extension: support for SQL99 savepoints
  2. Syntax
  3. ========
  4. SAVEPOINT <identifier>;
  5. Use the SAVEPOINT statement to identify a point in a transaction
  6. to which you can later roll back. <identifier> specifies the
  7. name of a savepoint to be created. Savepoint names must be
  8. distinct within a given transaction. If you create a second
  9. savepoint with the same identifer as an earlier savepoint,
  10. the earlier savepoint is erased. After a savepoint has been
  11. created, you can either continue processing, commit your work,
  12. roll back the entire transaction, or roll back to the savepoint.
  13. ROLLBACK [WORK] TO [SAVEPOINT] <identifier>;
  14. This statement performs the following operations:
  15. - Rolls back changes performed in the transaction after the savepoint
  16. - Erases all savepoints created after that savepoint. The named
  17. savepoint is retained, so you can roll back to the same savepoint
  18. multiple times. Prior savepoints are also retained.
  19. - Releases all implicit and explicit record locks acquired since the
  20. savepoint. Other transactions that have requested access to rows
  21. locked after the savepoint must continue to wait until the transaction
  22. is committed or rolled back. Other transactions that have not already
  23. requested the rows can request and access the rows immediately.
  24. Note: this behaviour may change in the future product versions.
  25. RELEASE SAVEPOINT <identifier> [ONLY];
  26. Use the RELEASE SAVEPOINT statement to erase savepoint <identifer>
  27. from the transaction context. Unless you specify ONLY clause all savepoints
  28. established since the savepoint <identifier> are erased too.
  29. Author:
  30. Nickolay Samofatov <[email protected]>
  31. (original implementation of user savepoints)
  32. Dmitry Yemanov <[email protected]>
  33. (refactored DSQL layer and BLR for better SQL99 and JDBC compliance)
  34. N O T E S
  35. =========
  36. 1. Using savepoints (alternate name is "nested transactions") is a very
  37. convenient method to handle business logic errors without rolling back
  38. the transaction.
  39. Common pattern for this (Java) is:
  40. stmt.executeUpdate("SAVEPOINT MyClass$do_some_work");
  41. try {
  42. MyClass.do_some_work();
  43. } catch(Exception ex) {
  44. stmt.executeUpdate("ROLLBACK TO MyClass$do_some_work");
  45. throw;
  46. }
  47. stmt.executeUpdate("RELEASE SAVEPOINT MyClass$do_some_work");
  48. 2. User savepoints are not supported in PSQL. Use traditional PSQL
  49. exception handling to undo changes performed in stored procedures
  50. and triggers. Support of user savepoints in PSQL layer would break
  51. the concept of statement atomicity (including procedure call statements).
  52. Each SQL/PSQL statement is executed under automatic system savepoint
  53. and either complete successfully or ALL its changes are rolled
  54. back and exception is raised. Each PSQL exception handling block is
  55. also bounded by automatic system savepoints.
  56. 3. Savepoint undo log may consume significant amounts of server
  57. memory exceptionally if you update the same records in the
  58. same transaction multiple times. Use RELEASE SAVEPOINT statement
  59. to release system resources required for savepoint maintenance.
  60. 4. By default, engine uses automatic transaction-level system
  61. savepoint to perform transaction rollback. When you issue
  62. ROLLBACK statement all changes performed in this transaction
  63. are backed out via transaction-level savepoint and transaction
  64. is committed then. This logic is used to reduce amount of garbage
  65. collection caused by rolled back transactions. When amount of
  66. changes performed under transaction-level savepoint is getting large
  67. (10^4-10^6 records affected) engine releases transaction-level
  68. savepoint and uses TIP mechanism to roll back the transaction if
  69. needed. You can use isc_tpb_no_auto_undo TPB flag to avoid creating
  70. transaction-level savepoint if you expect large amount of changes
  71. in your transaction.
  72. Example
  73. =======
  74. create table test (id integer);
  75. commit;
  76. insert into test values (1);
  77. commit;
  78. insert into test values (2);
  79. savepoint y;
  80. delete from test;
  81. select * from test; -- returns no rows
  82. rollback to y;
  83. select * from test; -- returns two rows
  84. rollback;
  85. select * from test; -- returns one row