> 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)

Database connection with DBAL module in Tempest

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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ecotone.tech/modules/tempest/database-connection-dbal-module.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
