> For the complete documentation index, see [llms.txt](https://docs.ecotone.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ecotone.tech/modules/tempest/database-connection-dbal-module.md).

# Database Connection (DBAL Module)

We can use Ecotone's Tempest integration to reuse the Connection that is already configured in your Application. This is what powers DBAL features such as the transactional outbox, dead letter, deduplication, document store, event sourcing and multi-tenant.

## **Using the existing Tempest Connection \[Recommended]**

In a Tempest application the database is configured through a `database.config.php` file, for example:

```php
use Tempest\Database\Config\PostgresConfig;

return new PostgresConfig(
    host: env('DB_HOST', 'localhost'),
    port: env('DB_PORT', '5432'),
    username: env('DB_USERNAME', 'app'),
    password: env('DB_PASSWORD', ''),
    database: env('DB_DATABASE', 'app'),
);
```

Using a `ServiceContext` we tell Ecotone to reuse this Connection as the default:

```php
use Ecotone\Messaging\Attribute\ServiceContext;
use Ecotone\Tempest\Config\TempestConnectionReference;

final class EcotoneConfiguration
{
    #[ServiceContext]
    public function tempestConnection(): TempestConnectionReference
    {
        return TempestConnectionReference::defaultConnection();
    }
}
```

{% hint style="success" %}
Reusing the same connection as your application uses ensures database transactions will be rolled back correctly in case of any failure — your Tempest model writes and Ecotone's DBAL operations share one connection.\
\
That's all that is needed. Ecotone will now use your Tempest database connection as the default.
{% endhint %}

## Multi-Tenant

For multi-tenant applications, register a tagged `DatabaseConfig` per tenant in your Tempest configuration, and map each tenant to a `TempestConnectionReference` by tag:

```php
use Ecotone\Dbal\MultiTenant\MultiTenantConfiguration;
use Ecotone\Messaging\Attribute\ServiceContext;
use Ecotone\Tempest\Config\TempestConnectionReference;

final class EcotoneConfiguration
{
    #[ServiceContext]
    public function multiTenant(): MultiTenantConfiguration
    {
        return MultiTenantConfiguration::create(
            tenantHeaderName: 'tenant',
            tenantToConnectionMapping: [
                'tenant_a' => TempestConnectionReference::create('tenant_a'),
                'tenant_b' => TempestConnectionReference::create('tenant_b'),
            ],
        );
    }
}
```

A command or query carrying the `tenant` header is then routed to the matching tenant database automatically.

{% hint style="info" %}
The DBAL features require the `ecotone/dbal` package: `composer require ecotone/dbal`.
{% endhint %}


---

# 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://docs.ecotone.tech/modules/tempest/database-connection-dbal-module.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.
