📄 lang_transaction.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>SQLite Query Language: BEGIN TRANSACTION</title><style type="text/css">body { margin: auto; font-family: "Verdana" "sans-serif"; padding: 8px 1%;}a { color: #45735f }a:visited { color: #734559 }.logo { position:absolute; margin:3px; }.tagline { float:right; text-align:right; font-style:italic; width:240px; margin:12px; margin-top:58px;}.toolbar { font-variant: small-caps; text-align: center; line-height: 1.6em; margin: 0; padding:1px 8px;}.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }.toolbar a:visited { color: white; }.toolbar a:hover { color: #80a796; background: white; }.content { margin: 5%; }.content dt { font-weight:bold; }.content dd { margin-bottom: 25px; margin-left:20%; }.content ul { padding:0px; padding-left: 15px; margin:0px; }/* rounded corners */.se { background: url(images/se.png) 100% 100% no-repeat #80a796}.sw { background: url(images/sw.png) 0% 100% no-repeat }.ne { background: url(images/ne.png) 100% 0% no-repeat }.nw { background: url(images/nw.png) 0% 0% no-repeat }</style><meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head><body><div><!-- container div to satisfy validator --><a href="index.html"><img class="logo" src="images/SQLite.gif" alt="SQLite Logo" border="0"></a><div><!-- IE hack to prevent disappearing logo--></div><div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div><table width=100% style="clear:both"><tr><td> <div class="se"><div class="sw"><div class="ne"><div class="nw"> <div class="toolbar"> <a href="about.html">About</a> <a href="sitemap.html">Sitemap</a> <a href="docs.html">Documentation</a> <a href="download.html">Download</a> <a href="copyright.html">License</a> <a href="news.html">News</a> <a href="http://www.sqlite.org/cvstrac/index">Developers</a> <a href="support.html">Support</a> </div></div></div></div></div></td></tr></table> <a href="lang.html"> <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 src="images/syntax/begin-stmt.gif"></img> </blockquote><h4><a href="syntaxdiagrams.html#commit-stmt">commit-stmt:</a></h4><blockquote> <img src="images/syntax/commit-stmt.gif"></img> </blockquote><h4><a href="syntaxdiagrams.html#rollback-stmt">rollback-stmt:</a></h4><blockquote> <img src="images/syntax/rollback-stmt.gif"></img> </blockquote><p>No changes can be made to the database except within a transaction.Any command that changes the database (basically, any SQL commandother than <a href="lang_select.html">SELECT</a>) will automatically start a transaction ifone is not already in effect. Automatically started transactionsare committed when the last query finishes.</p><p>Transactions can be started manually using the BEGINcommand. Such transactions usually persist until the nextCOMMIT or ROLLBACK command. But a transaction will also ROLLBACK if the database is closed or if an error occursand the ROLLBACK conflict resolution algorithm is specified.See the documentation on the <a href="lang_conflict.html">ON CONFLICT</a>clause for additional information about the ROLLBACKconflict resolution algorithm.</p><p>END TRANSACTION is an alias for COMMIT.</p><p>Transactions created using BEGIN...COMMIT do not nest.For nested transactions, use the <a href="lang_savepoint.html">SAVEPOINT</a> and <a href="lang_savepoint.html">RELEASE</a> commands.The "TO SAVEPOINT <i>name</i>" clause of the ROLLBACK command shownin the syntax diagram above is only applicable to <a href="lang_savepoint.html">SAVEPOINT</a>transactions. An attempt to invoke the BEGIN command withina transaction will fail with an error, regardless of whetherthe transaction was started by <a href="lang_savepoint.html">SAVEPOINT</a> or a prior BEGIN.The COMMIT command and the ROLLBACK command without the TO clausework the same on <a href="lang_savepoint.html">SAVEPOINT</a> transactions as they do with transactionsstarted by BEGIN.</p><p>Transactions can be deferred, immediate, or exclusive. The default transaction behavior is deferred.Deferred means that no locks are acquiredon the database until the database is first accessed. Thus with adeferred transaction, the BEGIN statement itself does nothing. Locksare not acquired until the first read or write operation. The first readoperation against a database creates a <a href="lockingv3.html#shared_lock">SHARED</a> lock and the firstwrite operation creates a <a href="lockingv3.html#reserved_lock">RESERVED</a> lock. Because the acquisition oflocks is deferred until they are needed, it is possible that anotherthread or process could create a separate transaction and write tothe database after the BEGIN on the current thread has executed.If the transaction is immediate, then <a href="lockingv3.html#reserved_lock">RESERVED</a> locksare acquired on all databases as soon as the BEGIN command isexecuted, without waiting for thedatabase to be used. After a BEGIN IMMEDIATE, you are guaranteed thatno other thread or process will be able to write to the database ordo a BEGIN IMMEDIATE or BEGIN EXCLUSIVE. Other processes can continueto read from the database, however. An exclusive transaction causes<a href="lockingv3.html#exclusive_lock">EXCLUSIVE</a> locks to be acquired on all databases. After a BEGINEXCLUSIVE, you are guaranteed that no other thread or process willbe able to read or write the database until the transaction iscomplete.</p><p>An implicit transaction (a transaction that is started automatically,not a transaction started by BEGIN) is committed automatically whenthe last active statement finishes. A statement finishes when itsprepared statement is <a href="c3ref/reset.html">reset</a> or<a href="c3ref/finalize.html">finalized</a>. An open <a href="c3ref/blob.html">sqlite3_blob</a> used forincremental BLOB I/O counts as an unfinished statement. The <a href="c3ref/blob.html">sqlite3_blob</a>finishes when it is <a href="c3ref/blob_close.html">closed</a>.</p><p>The explicit COMMIT command runs immediately, even if there arepending <a href="lang_select.html">SELECT</a> statements. However, if there are pendingwrite operations, the COMMIT commandwill fail with a error code <a href="c3ref/c_abort.html">SQLITE_BUSY</a>.</p><p>An attempt to execute COMMIT might also result in an <a href="c3ref/c_abort.html">SQLITE_BUSY</a> return codeif an another thread or process has a <a href="lockingv3.html#shared_lock">shared lock</a> on the databasethat prevented the database from being updated. When COMMIT fails in thisway, the transaction remains active and the COMMIT can be retried laterafter the reader has had a chance to clear.</p><p>The ROLLBACK will fail with an error code <a href="c3ref/c_abort.html">SQLITE_BUSY</a> if thereare any pending queries. Both read-only and read/write queries willcause a ROLLBACK to fail. A ROLLBACK must fail if there are pendingread operations (unlike COMMIT which can succeed) because bad thingswill happen if memory image of the database is changed out from underan active query.</p><h3>Response To Errors Within A Transaction</h3><p>If certain kinds of errors occur within a transaction, thetransaction may or may not be rolled back automatically. Theerrors that cause the behavior include:</p><ul><li> <a href="c3ref/c_abort.html">SQLITE_FULL</a>: database or disk full<li> <a href="c3ref/c_abort.html">SQLITE_IOERR</a>: disk I/O error<li> <a href="c3ref/c_abort.html">SQLITE_BUSY</a>: database in use by another process<li> <a href="c3ref/c_abort.html">SQLITE_NOMEM</a>: out or memory<li> <a href="c3ref/c_abort.html">SQLITE_INTERRUPT</a>: processing <a href="c3ref/interrupt.html">interrupted</a> by application request</ul><p>For all of these errors, SQLite attempts to undo just the one statementit was working on and leave changes from prior statements within thesame transaction intact and continue with the transaction. However, depending on the statement being evaluated and the point at which theerror occurs, it might be necessary for SQLite to rollback andcancel the entire transaction. An application can tell whichcourse of action SQLite took by using the<a href="c3ref/get_autocommit.html">sqlite3_get_autocommit()</a> C-language interface.</p><p>It is recommended that applications respond to the errorslisted above by explicitly issuing a ROLLBACK command. If the transaction has already been rolled back automaticallyby the error response, then the ROLLBACK command will fail with anerror, but no harm is caused by this.</p><p>Future versions of SQLite may extend the list of errors whichmight cause automatic transaction rollback. Future versions ofSQLite might change the error response. In particular, we maychoose to simplify the interface in future versions of SQLite bycausing the errors above to force an unconditional rollback.</p><hr><small><i>This page last modified 2009/01/02 16:47:13 UTC</i></small></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -