BEGIN, COMMIT, ROLLBACK
Starts a transaction, makes its changes permanent, or undoes them.
Access SQL. ACE runs BEGIN TRANSACTION, COMMIT TRANSACTION and ROLLBACK TRANSACTION. LibRed also takes
each word alone and with WORK.
Syntax
Section titled “Syntax”BEGIN [ TRANSACTION | WORK ]
COMMIT [ TRANSACTION | WORK ]
ROLLBACK [ TRANSACTION | WORK ]Arguments
Section titled “Arguments”TRANSACTION | WORK
Section titled “TRANSACTION | WORK”Optional words with no effect. BEGIN, BEGIN TRANSACTION and BEGIN WORK are the same statement, and so on
for the other two.
Remarks
Section titled “Remarks”BEGIN starts a transaction on the connection. The statements that follow are part of it until COMMIT keeps
their changes or ROLLBACK undoes them. A transaction covers DDL as well: a CREATE TABLE rolled back leaves no
table.
Transactions nest. A BEGIN inside a transaction starts an inner level, and COMMIT and ROLLBACK act on the
innermost level open. Rolling back an inner level undoes only its own work. Committing it keeps its work under
the outer level, which can still roll it back.
A statement that fails inside a transaction undoes only its own changes. The transaction stays open, and you can still commit or roll back the work before it.
COMMIT or ROLLBACK with no transaction open fails with “No transaction is in progress.”
Closing the connection with a transaction still open rolls it back.
Until it commits, a transaction’s changes are not written to the file, so no other connection can see them. LibRed is a single-writer engine; read Concurrency before you have two connections write to one file.
ADO.NET transactions
Section titled “ADO.NET transactions”The SQL statements and ADO.NET’s DbTransaction drive the same transaction. LibRedConnection.BeginTransaction()
does what BEGIN does, and Commit() and Rollback() what COMMIT and ROLLBACK do. You can mix the two on one
connection: an ADO.NET transaction started inside a SQL BEGIN is an inner level of it, and a SQL COMMIT run in
an ADO.NET transaction completes it, after which its Commit() fails.
In ADO.NET code, use BeginTransaction(), as EF Core does. Use the SQL statements where you only have SQL to
send.
using var connection = new LibRedConnection("Data Source=Northwind.accdb");connection.Open();using var transaction = connection.BeginTransaction();using var command = connection.CreateCommand();command.Transaction = transaction;command.CommandText = "UPDATE Products SET UnitPrice = UnitPrice * 2 WHERE CategoryID = 1";command.ExecuteNonQuery();transaction.Rollback(); // the prices are as they wereExamples
Section titled “Examples”These run against the Northwind sample database. They follow on from each other, and must run on one connection, since a transaction belongs to the connection that began it.
CREATE TABLE Ledger (Id COUNTER PRIMARY KEY, Entry TEXT(255))Keep one change and undo another:
BEGIN TRANSACTIONINSERT INTO Ledger (Entry) VALUES ('kept')COMMIT TRANSACTIONBEGIN TRANSACTIONINSERT INTO Ledger (Entry) VALUES ('undone')ROLLBACK TRANSACTIONLedger now holds only kept.
Roll back an inner level and commit the outer one:
BEGININSERT INTO Ledger (Entry) VALUES ('outer')BEGIN WORKINSERT INTO Ledger (Entry) VALUES ('inner')ROLLBACK WORKCOMMITLedger now holds kept and outer.