• Welcome to SQLitening Support Forum.
 

News:

Welcome to the SQLitening support forums!

Main Menu

Transaction Question

Started by D. Wilson, June 16, 2013, 07:55:07 PM

Previous topic - Next topic

D. Wilson

I have a 'stupid' question.. I just want to verify something before I jump in.

It is my understanding that I can combine both updates and inserts within one transaction. Ie insert 5 new records and update 20 records all within one transaction. Rather than creating two seperate transactions one for inserting and one for updating.

Paul Squires

Yes, you should be able to do that with no problems.

Paul Squires <br />http://www.planetsquires.com<br />support@planetsquires.com

D. Wilson

Based on my experience I have had no issues but I thought I would ask.
Also from my research I find the 'end' statement is acceptable as a 'commit' statement. (Which SQLite needs to complete the transaction).

Paul Squires

Right you are. "END" should be fine. I usually use syntax similar to this:
"BEGIN IMMEDIATE; <SQL STATEMENTS>; END TRANSACTION"

Paul Squires <br />http://www.planetsquires.com<br />support@planetsquires.com

cj

#4
Paul,
Why do you use BEGIN IMMEDIATE instead of letting SQLite do its normal processing with BEGIN?
I thought it made sense, but haven't seen others using it.

No changes can be made to the database except within a transaction. Any command that changes the database (basically, any SQL command other than SELECT) will automatically start a transaction if one is not already in effect. Automatically started transactions are committed when the last query finishes.

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 ON CONFLICT clause for additional information about the ROLLBACK conflict resolution algorithm.

END TRANSACTION is an alias for COMMIT.

Transactions created using BEGIN...COMMIT do not nest. For nested transactions, use the SAVEPOINT and RELEASE commands. The "TO SAVEPOINT name" clause of the ROLLBACK command shown in the syntax diagram above is only applicable to SAVEPOINT transactions. An attempt to invoke the BEGIN command within a transaction will fail with an error, regardless of whether the transaction was started by SAVEPOINT or a prior BEGIN. The COMMIT command and the ROLLBACK command without the TO clause work the same on SAVEPOINT transactions as they do with transactions started by BEGIN.

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 to the filesystem. Locks are not acquired until the first read or write operation. The first read operation against a database creates a SHARED lock and the first write operation creates a RESERVED 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 RESERVED 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, no other database connection 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 EXCLUSIVE locks to be acquired on all databases. After a BEGIN EXCLUSIVE, no other database connection except for read_uncommitted connections will be able to read the database and no other connection without exception will be able to write the database until the transaction is complete.

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.

The explicit COMMIT command runs immediately, even if there are pending SELECT statements. However, if there are pending write operations, the COMMIT command will fail with an error code SQLITE_BUSY.

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.

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.

If PRAGMA journal_mode is set to OFF (thus disabling the rollback journal file) then the behavior of the ROLLBACK command is undefined.

Rolf Brandt

I like to cook with wine - sometimes I even add it to the food.
www.rbsoft.eu

cj

Thank you very much! 
It could definitely avoid a deadlock with BEGIN/BEGIN DEFERRED!

Bern Ertl

Quote from: cj on June 17, 2013, 02:06:56 PM
Paul,
Why do you use BEGIN IMMEDIATE instead of letting SQLite do its normal processing with BEGIN?
I thought it made sense, but haven't seen others using it.
...

I use it frequently.  I also use BEGIN EXCLUSIVE in some cases as needed when I need to lock out a portion of the database while doing some heavy calculations (mostly within server side SQLitening PROC functions).

When you have several updates/inserts occurring in a loop, sandwiching the code with BEGIN IMMEDIATE  / COMMIT will improve efficiency/performance.