> For the complete documentation index, see [llms.txt](https://clear.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://clear.gitbook.io/project/model/transactions-and-save-points/transaction.md).

# 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:

```ruby
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:

```ruby
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:

```ruby
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:

```ruby
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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://clear.gitbook.io/project/model/transactions-and-save-points/transaction.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
