# Transactions

{% 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.**

***
