123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <!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="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 alt="syntax diagram begin-stmt" src="images/syntax/begin-stmt.gif"></img> </blockquote>
- <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>
- <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>
- <p>
- No changes can be made to the database except within a transaction.
- Any command that changes the database (basically, any SQL command
- other than <a href="lang_select.html">SELECT</a>) will automatically start a transaction if
- one is not already in effect. Automatically started transactions
- are committed when the last query finishes.
- </p>
- <p>
- Transactions can be started manually using the BEGIN
- command. Such transactions usually persist until the next
- COMMIT or ROLLBACK command. But a transaction will also
- ROLLBACK if the database is closed or if an error occurs
- and 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 ROLLBACK
- conflict 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 shown
- in the syntax diagram above is only applicable to <a href="lang_savepoint.html">SAVEPOINT</a>
- transactions. An attempt to invoke the BEGIN command within
- a transaction will fail with an error, regardless of whether
- the 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 clause
- work the same on <a href="lang_savepoint.html">SAVEPOINT</a> transactions as they do with transactions
- started by BEGIN.</p>
- <p>
- Transactions can be deferred, immediate, or exclusive.
- The default transaction behavior is deferred.
- Deferred means that no locks are acquired
- on the database until the database is first accessed. Thus with a
- deferred transaction, the BEGIN statement itself does nothing. Locks
- are not acquired until the first read or write operation. The first read
- operation against a database creates a <a href="lockingv3.html#shared_lock">SHARED</a> lock and the first
- write operation creates a <a href="lockingv3.html#reserved_lock">RESERVED</a> lock. Because the acquisition of
- locks is deferred until they are needed, it is possible that another
- thread or process could create a separate transaction and write to
- the database after the BEGIN on the current thread has executed.
- If the transaction is immediate, then <a href="lockingv3.html#reserved_lock">RESERVED</a> locks
- are acquired on all databases as soon as the BEGIN command is
- executed, without waiting for the
- database to be used. After a BEGIN IMMEDIATE, you are guaranteed that
- no other thread or process will be able to write to the database or
- do a BEGIN IMMEDIATE or BEGIN EXCLUSIVE. Other processes can continue
- to 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 BEGIN
- EXCLUSIVE, you are guaranteed that no other thread or process will
- be able to read or write the database until the transaction is
- complete.
- </p>
- <p>
- An implicit transaction (a transaction that is started automatically,
- not a transaction started by BEGIN) is committed automatically when
- the last active statement finishes. A statement finishes when its
- prepared statement is reset or
- finalized. An open sqlite3_blob used for
- incremental BLOB I/O counts as an unfinished statement. The sqlite3_blob
- finishes when it is closed.
- </p>
- <p>
- The explicit COMMIT command runs immediately, even if there are
- pending <a href="lang_select.html">SELECT</a> statements. However, if there are pending
- write operations, the COMMIT command
- will fail with a error code SQLITE_BUSY.
- </p>
- <p>
- An attempt to execute COMMIT might also result in an SQLITE_BUSY return code
- if an another thread or process has a shared lock on the database
- that prevented the database from being updated. When COMMIT fails in this
- way, the transaction remains active and the COMMIT can be retried later
- after the reader has had a chance to clear.
- </p>
- <p>
- The ROLLBACK will fail with an error code SQLITE_BUSY if there
- are any pending queries. Both read-only and read/write queries will
- cause a ROLLBACK to fail. A ROLLBACK must fail if there are pending
- read operations (unlike COMMIT which can succeed) because bad things
- will happen if the in-memory image of the database is changed out from under
- an active query.
- </p>
- <p>
- If <a href="pragma.html#pragma_journal_mode">PRAGMA journal_mode</a> is set to OFF (thus disabling the rollback journal
- file) then the behavior of the ROLLBACK command is undefined.
- </p>
- <h3>Response To Errors Within A Transaction</h3>
- <p>If certain kinds of errors occur within a transaction, the
- transaction may or may not be rolled back automatically. The
- errors that cause the behavior include:</p>
- <ul>
- <li> SQLITE_FULL: database or disk full
- <li> SQLITE_IOERR: disk I/O error
- <li> SQLITE_BUSY: database in use by another process
- <li> SQLITE_NOMEM: out or memory
- <li> SQLITE_INTERRUPT: processing interrupted
- by application request
- </ul>
- <p>
- For all of these errors, SQLite attempts to undo just the one statement
- it was working on and leave changes from prior statements within the
- same transaction intact and continue with the transaction. However,
- depending on the statement being evaluated and the point at which the
- error occurs, it might be necessary for SQLite to rollback and
- cancel the entire transaction. An application can tell which
- course of action SQLite took by using the
- sqlite3_get_autocommit() C-language interface.</p>
- <p>It is recommended that applications respond to the errors
- listed above by explicitly issuing a ROLLBACK command. If the
- transaction has already been rolled back automatically
- by the error response, then the ROLLBACK command will fail with an
- error, but no harm is caused by this.</p>
- <p>Future versions of SQLite may extend the list of errors which
- might cause automatic transaction rollback. Future versions of
- SQLite might change the error response. In particular, we may
- choose to simplify the interface in future versions of SQLite by
- causing the errors above to force an unconditional rollback.</p>
|