module Clear::SQL::Transaction

Direct including types

Defined in:

clear/sql/transaction.cr

Instance Method Summary

Instance Method Detail

def 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!
  Clear::SQL.after_commit { Email.deliver(ConfirmationMail.new(@user)) }
end

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


[View source]
def in_transaction?(connection : String = "default") #

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


[View source]
def rollback(to = nil) #

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


[View source]
def rollback_transaction #

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


[View source]
def transaction(connection : String = "default", level : Level = Level::Serializable, &) #

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

Example:

Clear::SQL.transaction do
  # do something
  Clear::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.


[View source]
def with_savepoint(sp_name : Symbolic | Nil = nil, connection_name : String = "default", &) #

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

Example:

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

[View source]