2
0

lang_savepoint.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html><head>
  3. <title>SQLite Query Language: SAVEPOINT</title>
  4. <style type="text/css">
  5. body {
  6. margin: auto;
  7. font-family: Verdana, sans-serif;
  8. padding: 8px 1%;
  9. }
  10. a { color: #45735f }
  11. a:visited { color: #734559 }
  12. .logo { position:absolute; margin:3px; }
  13. .tagline {
  14. float:right;
  15. text-align:right;
  16. font-style:italic;
  17. width:240px;
  18. margin:12px;
  19. margin-top:58px;
  20. }
  21. .toolbar {
  22. font-variant: small-caps;
  23. text-align: center;
  24. line-height: 1.6em;
  25. margin: 0;
  26. padding:1px 8px;
  27. }
  28. .toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
  29. .toolbar a:visited { color: white; }
  30. .toolbar a:hover { color: #80a796; background: white; }
  31. .content { margin: 5%; }
  32. .content dt { font-weight:bold; }
  33. .content dd { margin-bottom: 25px; margin-left:20%; }
  34. .content ul { padding:0px; padding-left: 15px; margin:0px; }
  35. /* rounded corners */
  36. .se { background: url(images/se.png) 100% 100% no-repeat #80a796}
  37. .sw { background: url(images/sw.png) 0% 100% no-repeat }
  38. .ne { background: url(images/ne.png) 100% 0% no-repeat }
  39. .nw { background: url(images/nw.png) 0% 0% no-repeat }
  40. </style>
  41. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  42. </head>
  43. <body>
  44. <div><!-- container div to satisfy validator -->
  45. <a href="lang.html">
  46. <h2 align="center">SQL As Understood By SQLite</h2></a><h1>SAVEPOINT</h1><h4><a href="syntaxdiagrams.html#savepoint-stmt">savepoint-stmt:</a></h4><blockquote> <img alt="syntax diagram savepoint-stmt" src="images/syntax/savepoint-stmt.gif"></img> </blockquote>
  47. <h4><a href="syntaxdiagrams.html#release-stmt">release-stmt:</a></h4><blockquote> <img alt="syntax diagram release-stmt" src="images/syntax/release-stmt.gif"></img> </blockquote>
  48. <h4><a href="syntaxdiagrams.html#rollback-stmt">rollback-stmt:</a></h4><blockquote> <img alt="syntax diagram rollback-stmt" src="images/syntax/rollback-stmt.gif"></img> </blockquote>
  49. <p>SAVEPOINTs are a method of creating transactions, similar to
  50. <a href="lang_transaction.html">BEGIN</a> and <a href="lang_transaction.html">COMMIT</a>, except that the SAVEPOINT and RELEASE commands
  51. are named and may be nested.</p>
  52. <p>The SAVEPOINT command starts a new transaction with a name.
  53. The transaction names need not be unique.
  54. A SAVEPOINT can be started either within or outside of
  55. a <a href="lang_transaction.html">BEGIN</a>...<a href="lang_transaction.html">COMMIT</a>. When a SAVEPOINT is the outer-most savepoint
  56. and it is not within a <a href="lang_transaction.html">BEGIN</a>...<a href="lang_transaction.html">COMMIT</a> then the behavior is the
  57. same as BEGIN DEFERRED TRANSACTION.</p>
  58. <p>The ROLLBACK TO command reverts the state of the database back to what
  59. it was just after the corresponding SAVEPOINT. Note that unlike that
  60. plain <a href="lang_transaction.html">ROLLBACK</a> command (without the TO keyword) the ROLLBACK TO command
  61. does not cancel the transaction. Instead of cancelling the transaction,
  62. the ROLLBACK TO command restarts the transaction again at the beginning.
  63. All intervening SAVEPOINTs are cancelled, however.</p>
  64. <p>The RELEASE is like a <a href="lang_transaction.html">COMMIT</a> for a SAVEPOINT.
  65. The RELEASE command causes all savepoints back to and including the first
  66. savepoint with a matching name to be removed from the transaction stack. The
  67. RELEASE of an inner transaction
  68. does not cause any changes to be written to the database file; it merely
  69. removes savepoints from the transaction stack such that it is
  70. no longer possible to ROLLBACK TO those savepoints.
  71. If a RELEASE command releases the outermost savepoint, so
  72. that the transaction stack becomes empty, then RELEASE is the same
  73. as <a href="lang_transaction.html">COMMIT</a>.
  74. The <a href="lang_transaction.html">COMMIT</a> command may used to release all savepoints and
  75. commit the transaction even if the transaction was originally started
  76. by a SAVEPOINT command instead of a <a href="lang_transaction.html">BEGIN</a> command.</p>
  77. <p>If the savepoint-name in a RELEASE command does not match any
  78. savepoint currently in the tranaction stack, then no savepoints are
  79. released, the database is unchanged, and the RELEASE command returns
  80. an error.</p>
  81. <p>Note that an inner transaction might commit (using the RELEASE command)
  82. but then later have its work undone by a ROLLBACK in an outer transaction.
  83. A power failure or program crash or OS crash will cause the outer-most
  84. transaction to rollback, undoing all changes that have occurred within
  85. that outer transaction, even changes that have supposedly been "committed"
  86. by the RELEASE command. Content is not actually committed on the disk
  87. until the outermost transaction commits.</p>
  88. <p>There are several ways of thinking about the RELEASE command:</p>
  89. <ul>
  90. <li><p>
  91. Some people view RELEASE as the equivalent of COMMIT for a SAVEPOINT.
  92. This is an acceptable point of view as long as one remembers that the
  93. changes committed by an inner transaction might later be undone by a
  94. rollback in an outer transaction.</p></li>
  95. <li><p>
  96. Another view of RELEASE is that it merges a named transaction into its
  97. parent transaction, so that the named transaction and its parent become
  98. the same transaction. After RELEASE, the named transaction and its parent
  99. will commit or rollback together, whatever their fate may be.
  100. </p></li>
  101. <li><p>
  102. One can also think of savepoints as
  103. "marks" in the transaction timeline. In this view, the SAVEPOINT command
  104. creates a new mark, the ROLLBACK TO command rewinds the timeline back
  105. to a point just after the named mark, and the RELEASE command
  106. erases marks from the timeline without actually making any
  107. changes to the database.
  108. </p></li>
  109. </ul>
  110. <h3>Transaction Nesting Rules</h3>
  111. <p>Transactions stack. The last transaction started will be the first
  112. transaction committed or rolled back.</p>
  113. <p>The <a href="lang_transaction.html">BEGIN</a> command only works if the transaction stack is empty, or
  114. in other words if there are no pending transactions. If the transaction
  115. stack is not empty when the <a href="lang_transaction.html">BEGIN</a> command is invoked, then the command
  116. fails with an error.</p>
  117. <p>The <a href="lang_transaction.html">COMMIT</a> command commits all outstanding transactions and leaves
  118. the transaction stack empty.</p>
  119. <p>The RELEASE command starts with the most recent addition to the
  120. transaction stack and releases savepoints backwards
  121. in time until it releases a savepoint mark with a matching savepoint-name.
  122. Prior savepoints, even savepoints with matching savepoint-names, are
  123. unchanged.
  124. If the RELEASE command causes the
  125. transaction stack to become empty (if the RELEASE command releases the
  126. outermost transaction from the stack) then the transaction commits.</p>
  127. <p>The <a href="lang_transaction.html">ROLLBACK</a> command without a TO clause rolls backs all transactions
  128. and leaves the transaction stack empty.</p>
  129. <p>The ROLLBACK command with a TO clause rolls back transactions going
  130. backwards in time back to the most recent SAVEPOINT with a matching name.
  131. The SAVEPOINT with the matching name remains on the transaction stack,
  132. but all database changes that occurred after that SAVEPOINT was created
  133. are rolled back. If the savepoint-name in a ROLLBACK TO command does not
  134. match any SAVEPOINT on the stack, then the ROLLBACK command fails with an
  135. error and leaves the state of the database unchanged.</p>