# Transaction Management {#transaction-management}


Handle database transactions for data consistency.


## TypeORM Transactions {#typeorm-transactions}



### Using QueryRunner {#using-queryrunner}


```typescript
async transferFunds(fromId: string, toId: string, amount: number) {
  const queryRunner = this.dataSource.createQueryRunner();
  await queryRunner.connect();
  await queryRunner.startTransaction();

  try {
    await queryRunner.manager.decrement(Account, { id: fromId }, 'balance', amount);
    await queryRunner.manager.increment(Account, { id: toId }, 'balance', amount);

    await queryRunner.commitTransaction();
  } catch (err) {
    await queryRunner.rollbackTransaction();
    throw err;
  } finally {
    await queryRunner.release();
  }
}
```


### Using @Transaction Decorator {#using-transaction-decorator}


```typescript
@Transaction()
async createInvoiceWithItems(
  @TransactionManager() manager: EntityManager,
  dto: CreateInvoiceDTO
) {
  const invoice = await manager.save(Invoice, dto);
  const items = dto.items.map(item => ({
    ...item,
    invoiceId: invoice.id,
  }));
  await manager.save(InvoiceItem, items);
  return invoice;
}
```


### Using EntityManager {#using-entitymanager}


```typescript
await this.dataSource.transaction(async (manager) => {
  const employee = await manager.save(Employee, employeeData);
  const user = await manager.save(User, {
    ...userData,
    employeeId: employee.id,
  });
  return { employee, user };
});
```


## Transaction Isolation Levels {#transaction-isolation-levels}


| Level            | Dirty Read | Non-Repeatable | Phantom |
| ---------------- | ---------- | -------------- | ------- |
| READ UNCOMMITTED | ✅         | ✅             | ✅      |
| READ COMMITTED   | ❌         | ✅             | ✅      |
| REPEATABLE READ  | ❌         | ❌             | ✅      |
| SERIALIZABLE     | ❌         | ❌             | ❌      |

```typescript
await this.dataSource.transaction("SERIALIZABLE", async (manager) => {
  // Strictest isolation
});
```


## Best Practices {#best-practices}


- Keep transactions short
- Avoid long-running queries inside transactions
- Handle deadlocks with retry logic
- Use the lowest isolation level that ensures correctness


## Related Pages {#related-pages}


- [Database Schema](https://sourcey-gauzy.pages.dev/database/schema-overview/index.md) — schema
- [Query Builder](https://sourcey-gauzy.pages.dev/database/query-builder/index.md) — complex queries
- [Connection Pooling](https://sourcey-gauzy.pages.dev/database/connection-pooling/index.md) — pool management
