> For the complete documentation index, see [llms.txt](https://cross-packages.gitbook.io/cross-api/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cross-packages.gitbook.io/cross-api/pt-br/services/transactions.md).

# Transactions

{% code fullWidth="false" %}

```typescript
class Example {
  public async handle(connection: IConnection) {
    // Cria um queryRunner
    const trx = connection.mysql.createQueryRunner();

    // Cria uma única conexão com o banco de dados
    await trx.startTransaction();
    try {
      
      // O objeto trx é obrigatório se houver uma conexão com um banco de dados, mas opcional se você estiver usando "em repositórios de memória"
      const result = await this.examplesRepository.create({ name: 'example' }, trx);

      // Use depois de toda a lógica e antes que o método retorne para persistir modificações no banco de dados
      if (trx.isTransactionActive) await trx.commitTransaction();

      return result;
    } catch (error: unknown) {
      // Em caso de falha de execução, reverte todas as alterações feitas no banco de dados
      if (trx.isTransactionActive) await trx.rollbackTransaction();
      throw error;
    } finally {
      // Libera a conexão para que possa ser usada em outros casos
      if (!trx.isReleased) await trx.release();
    }
  }
}
```

{% endcode %}

Existem 13 tipos de consultas padrão para todos os módulos, elas são totalmente dinâmicas para que respondam a 90% de suas necessidades.

***


---

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

```
GET https://cross-packages.gitbook.io/cross-api/pt-br/services/transactions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
