module

Lustra::SQL::Transaction

Instance methods

after_commit(connection : String = "default", &block : DB::Connection -> Nil)

Register a callback function which will be fired once when SQL COMMIT operation is called

This can be used for example to send email, or perform others tasks when you want to be sure the data is secured in the database.

transaction do
  @user = User.find(1)
  @user.subscribe!
  Lustra::SQL.after_commit { Email.deliver(ConfirmationMail.new(@user)) }
end

In case the transaction fail and eventually rollback, the code won't be called.

Source
in_transaction?(connection : String = "default")

Check whether the current pair of fiber/connection is in transaction block or not.

Source
rollback(to = nil)

Rollback a transaction or return to the previous savepoint in case of a with_savepoint block. The params to offer

Source
rollback_transaction

Rollback the transaction. In case the call is made inside a savepoint block rollback everything.

Source
transaction(connection : String = "default", level : Level = Level::Serializable, &)

Enter new transaction block for the current connection/fiber pair.

Example:

Lustra::SQL.transaction do
  # do something
  Lustra::SQL.transaction do # Technically, this block do nothing, since we already are in transaction
    rollback                 # < Rollback the up-most `transaction` block.
  end
end

see #with_savepoint to use a stackable version using savepoints.

Source
with_savepoint(sp_name : Symbolic | Nil = nil, connection_name : String = "default", &)

Create a transaction, but this one is stackable using savepoints.

Example:

Lustra::SQL.with_savepoint do
  # do something
  Lustra::SQL.with_savepoint do
    rollback # < Rollback only the last `with_savepoint` block
  end
end
Source

Nested types