> 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/services/transactions.md).

# Transactions

By introducing the transactions, they revert all changes made to the database in the event that the method fails to execute. This helps prevent traces of data considered junk.

{% code fullWidth="false" %}

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

    // Creates a single connection to the database
    await trx.startTransaction();
    try {
      
      // The trx object is mandatory if there is a connection to a database but optional if you are using "in memory repositories"
      const result = await this.examplesRepository.create({ name: 'example' }, trx);

      // Use after all the logic and before the method returns to persist modifications to the database
      if (trx.isTransactionActive) await trx.commitTransaction();

      return result;
    } catch (error: unknown) {
      // In case of execution failure rolls back all changes made to the database
      if (trx.isTransactionActive) await trx.rollbackTransaction();
      throw error;
    } finally {
      // Releases the connection so it can be used in other cases
      if (!trx.isReleased) await trx.release();
    }
  }
}
```

{% endcode %}

**There are 13 types of standard queries for all modules, they are fully dynamic so they will respond to 90% of your needs.**

***
