2
0

lang_transaction.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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: BEGIN TRANSACTION</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>BEGIN TRANSACTION</h1><h4><a href="syntaxdiagrams.html#begin-stmt">begin-stmt:</a></h4><blockquote> <img alt="syntax diagram begin-stmt" src="images/syntax/begin-stmt.gif"></img> </blockquote>
  47. <h4><a href="syntaxdiagrams.html#commit-stmt">commit-stmt:</a></h4><blockquote> <img alt="syntax diagram commit-stmt" src="images/syntax/commit-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>
  50. No changes can be made to the database except within a transaction.
  51. Any command that changes the database (basically, any SQL command
  52. other than <a href="lang_select.html">SELECT</a>) will automatically start a transaction if
  53. one is not already in effect. Automatically started transactions
  54. are committed when the last query finishes.
  55. </p>
  56. <p>
  57. Transactions can be started manually using the BEGIN
  58. command. Such transactions usually persist until the next
  59. COMMIT or ROLLBACK command. But a transaction will also
  60. ROLLBACK if the database is closed or if an error occurs
  61. and the ROLLBACK conflict resolution algorithm is specified.
  62. See the documentation on the <a href="lang_conflict.html">ON CONFLICT</a>
  63. clause for additional information about the ROLLBACK
  64. conflict resolution algorithm.
  65. </p>
  66. <p>
  67. END TRANSACTION is an alias for COMMIT.
  68. </p>
  69. <p>Transactions created using BEGIN...COMMIT do not nest.
  70. For nested transactions, use the <a href="lang_savepoint.html">SAVEPOINT</a> and <a href="lang_savepoint.html">RELEASE</a> commands.
  71. The "TO SAVEPOINT <i>name</i>" clause of the ROLLBACK command shown
  72. in the syntax diagram above is only applicable to <a href="lang_savepoint.html">SAVEPOINT</a>
  73. transactions. An attempt to invoke the BEGIN command within
  74. a transaction will fail with an error, regardless of whether
  75. the transaction was started by <a href="lang_savepoint.html">SAVEPOINT</a> or a prior BEGIN.
  76. The COMMIT command and the ROLLBACK command without the TO clause
  77. work the same on <a href="lang_savepoint.html">SAVEPOINT</a> transactions as they do with transactions
  78. started by BEGIN.</p>
  79. <p>
  80. Transactions can be deferred, immediate, or exclusive.
  81. The default transaction behavior is deferred.
  82. Deferred means that no locks are acquired
  83. on the database until the database is first accessed. Thus with a
  84. deferred transaction, the BEGIN statement itself does nothing. Locks
  85. are not acquired until the first read or write operation. The first read
  86. operation against a database creates a <a href="lockingv3.html#shared_lock">SHARED</a> lock and the first
  87. write operation creates a <a href="lockingv3.html#reserved_lock">RESERVED</a> lock. Because the acquisition of
  88. locks is deferred until they are needed, it is possible that another
  89. thread or process could create a separate transaction and write to
  90. the database after the BEGIN on the current thread has executed.
  91. If the transaction is immediate, then <a href="lockingv3.html#reserved_lock">RESERVED</a> locks
  92. are acquired on all databases as soon as the BEGIN command is
  93. executed, without waiting for the
  94. database to be used. After a BEGIN IMMEDIATE, you are guaranteed that
  95. no other thread or process will be able to write to the database or
  96. do a BEGIN IMMEDIATE or BEGIN EXCLUSIVE. Other processes can continue
  97. to read from the database, however. An exclusive transaction causes
  98. <a href="lockingv3.html#exclusive_lock">EXCLUSIVE</a> locks to be acquired on all databases. After a BEGIN
  99. EXCLUSIVE, you are guaranteed that no other thread or process will
  100. be able to read or write the database until the transaction is
  101. complete.
  102. </p>
  103. <p>
  104. An implicit transaction (a transaction that is started automatically,
  105. not a transaction started by BEGIN) is committed automatically when
  106. the last active statement finishes. A statement finishes when its
  107. prepared statement is reset or
  108. finalized. An open sqlite3_blob used for
  109. incremental BLOB I/O counts as an unfinished statement. The sqlite3_blob
  110. finishes when it is closed.
  111. </p>
  112. <p>
  113. The explicit COMMIT command runs immediately, even if there are
  114. pending <a href="lang_select.html">SELECT</a> statements. However, if there are pending
  115. write operations, the COMMIT command
  116. will fail with a error code SQLITE_BUSY.
  117. </p>
  118. <p>
  119. An attempt to execute COMMIT might also result in an SQLITE_BUSY return code
  120. if an another thread or process has a shared lock on the database
  121. that prevented the database from being updated. When COMMIT fails in this
  122. way, the transaction remains active and the COMMIT can be retried later
  123. after the reader has had a chance to clear.
  124. </p>
  125. <p>
  126. The ROLLBACK will fail with an error code SQLITE_BUSY if there
  127. are any pending queries. Both read-only and read/write queries will
  128. cause a ROLLBACK to fail. A ROLLBACK must fail if there are pending
  129. read operations (unlike COMMIT which can succeed) because bad things
  130. will happen if the in-memory image of the database is changed out from under
  131. an active query.
  132. </p>
  133. <p>
  134. If <a href="pragma.html#pragma_journal_mode">PRAGMA journal_mode</a> is set to OFF (thus disabling the rollback journal
  135. file) then the behavior of the ROLLBACK command is undefined.
  136. </p>
  137. <h3>Response To Errors Within A Transaction</h3>
  138. <p>If certain kinds of errors occur within a transaction, the
  139. transaction may or may not be rolled back automatically. The
  140. errors that cause the behavior include:</p>
  141. <ul>
  142. <li> SQLITE_FULL: database or disk full
  143. <li> SQLITE_IOERR: disk I/O error
  144. <li> SQLITE_BUSY: database in use by another process
  145. <li> SQLITE_NOMEM: out or memory
  146. <li> SQLITE_INTERRUPT: processing interrupted
  147. by application request
  148. </ul>
  149. <p>
  150. For all of these errors, SQLite attempts to undo just the one statement
  151. it was working on and leave changes from prior statements within the
  152. same transaction intact and continue with the transaction. However,
  153. depending on the statement being evaluated and the point at which the
  154. error occurs, it might be necessary for SQLite to rollback and
  155. cancel the entire transaction. An application can tell which
  156. course of action SQLite took by using the
  157. sqlite3_get_autocommit() C-language interface.</p>
  158. <p>It is recommended that applications respond to the errors
  159. listed above by explicitly issuing a ROLLBACK command. If the
  160. transaction has already been rolled back automatically
  161. by the error response, then the ROLLBACK command will fail with an
  162. error, but no harm is caused by this.</p>
  163. <p>Future versions of SQLite may extend the list of errors which
  164. might cause automatic transaction rollback. Future versions of
  165. SQLite might change the error response. In particular, we may
  166. choose to simplify the interface in future versions of SQLite by
  167. causing the errors above to force an unconditional rollback.</p>