Transactions
To run queries in a transaction, wrap the code in a transaction
block:
dbConn.transaction:
for i in 11..13:
discard newUser($i & "@example.com").dup:
dbConn.insert
echo()
BEGIN INSERT INTO "User" (email) VALUES(?) <- @['11@example.com'] INSERT INTO "User" (email) VALUES(?) <- @['12@example.com'] INSERT INTO "User" (email) VALUES(?) <- @['13@example.com'] COMMIT
If something goes wrong inside a transaction block, i.e. an exception is raised, the transaction is rollbacked.
To rollback a transaction manually, call rollback
proc:
try:
dbConn.transaction:
for i in 14..16:
discard newUser($i & "@example.com").dup:
dbConn.insert
if i == 15:
rollback()
except RollbackError:
echo getCurrentExceptionMsg()
BEGIN INSERT INTO "User" (email) VALUES(?) <- @['14@example.com'] INSERT INTO "User" (email) VALUES(?) <- @['15@example.com'] ROLLBACK Rollback transaction.