Clear ORM
  • Welcome to Clear
  • Introduction
    • Setup
  • Model
    • Defining your model
      • Describing your columns
      • Primary Keys
      • Converters
    • Associations
      • belongs_to
      • has_many
      • has_many through
      • has_one
    • Lifecycle
      • Persistence
      • Validations
      • Triggers
    • Batchs operations
      • Bulk update
      • Bulk insert & delete
    • Transactions & Save Points
      • Transaction & Savepoints
      • Connection pool
    • Locks
  • Querying
    • The collection object
      • Filter the query
        • Filter the query – The Expression Engine
        • Find, First, Last, Offset, Limit
        • Aggregation
        • Ordering & Group by
      • Fetching the query
        • Each and Fetch
        • Cursored fetching
        • Model extra attributes
      • Joins
      • Eager Loading
      • Window and CTE
      • Scopes
    • Writing low-level SQL
      • Select Clause
      • Insert Clause
      • Delete Clause
  • Migrations
    • Manage migrations
    • Call migration script
    • Migration CLI
  • Additional and advanced features
    • JSONB
    • Symbol vs String
    • Enums
    • BCrypt
    • Full Text Search
    • Handling multi-connection
  • Other resources
    • API Documentation
    • Inline documentation
    • Github repository
    • Credits
    • Benchmark
Powered by GitBook
On this page
  • Rollback
  • Nested transaction
  • Savepoints
  1. Model
  2. Transactions & Save Points

Transaction & Savepoints

Transaction are safeguard to ensure than a list of operation on a database are only permanent if they can all succeed as atomic action.

In Clear, the usage of transaction is simple:

Clear::SQL.transaction do
    yacine.withdraw(100)
    mary.deposit(100)
end

In the example above, if one of the method fail, the whole transaction block will be reverted to initial state.

Rollback

You can manually rollback a transaction if something went wrong:

Clear::SQL.transaction do
    yacine.withdraw(100)
    Clear::SQL.rollback if mary.is_suspicious?
    mary.deposit(100)
end

In this case, the block will be returned, nothing will be committed in the database and no error will be thrown

Nested transaction

Nested transaction are not working, but save points are used for that. Let's take an example:

Clear::SQL.transaction do
    puts "I do something"
    Clear::SQL.transaction do
        puts "I do another thing"
        Clear::SQL.rollback
        puts "This should not print"
    end
    puts "This will never reach too."
end

In this case, the output will be:

# BEGIN
I do something
I do another thing
# ROLLBACK

Since nested transaction are not permitted, rollback will rollback the top-most transaction. Any nested transaction block will perform SQL-wise, only the block content will be executed.

Savepoints

For nested transaction, you may want to use save points:

Clear::SQL.with_savepoint do
    puts "I do something"
    Clear::SQL.with_savepoint do
        puts "I do another thing"
        Clear::SQL.rollback
        puts "This should not print"
    end
    puts "Eventually, I do something else"
end

In this case, the output will be:

# BEGIN
# SAVEPOINT xxx1
I do something
# SAVEPOINT xxx2
I do another thing
# ROLLBACK TO SAVEPOINT xxx2
Eventually, I do something else
# RELEASE SAVEPOINT xxx1
# COMMIT

As you can see, save points are backed by a transaction block; rollback inside a save point block will rollback the block only and not all the transaction. Any unhandled exception will still rollback the full transaction.

PreviousTransactions & Save PointsNextConnection pool

Last updated 6 years ago