> For the complete documentation index, see [llms.txt](https://brightercommand.gitbook.io/paramore-brighter-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://brightercommand.gitbook.io/paramore-brighter-documentation/outbox-and-inbox/distributedlock/mysqldistributedlock.md).

# MySQL Distributed Lock

The MySQL locking provider implements Brighter's [distributed lock](/paramore-brighter-documentation/outbox-and-inbox/distributedlock.md) using MySQL's named locks (`GET_LOCK` / `RELEASE_LOCK`), so a single [Outbox Sweeper](/paramore-brighter-documentation/outbox-and-inbox/brighteroutboxsupport.md#implicit-clear) and Archiver run when you scale out. It pairs naturally with the [MySQL Outbox](/paramore-brighter-documentation/outbox-and-inbox/mysqloutbox.md).

## Package

* **Paramore.Brighter.Locking.MySql**

Named locks are a built-in MySQL feature, so there is **no table to provision** — see [Provisioning](#provisioning).

## Configuration

Like the MS SQL provider, the MySQL provider does not take an options class. You construct it with a `MySqlConnectionProvider`, which it uses to open the session that holds the lock. The connection provider is built from an `IAmARelationalDatabaseConfiguration` (a `RelationalDatabaseConfiguration`) carrying your connection string:

```csharp
var configuration = new RelationalDatabaseConfiguration(
    connectionString: "Server=localhost;Database=orders;Uid=app;Pwd=secret;");

var lockingProvider = new MySqlLockingProvider(new MySqlConnectionProvider(configuration));
```

There are no lease settings to tune — the lock is held for the lifetime of the database session.

## Example

```csharp
var configuration = new RelationalDatabaseConfiguration(
    "Server=localhost;Database=orders;Uid=app;Pwd=secret;");

services
    .AddBrighter()
    .AddProducers(opt =>
    {
        opt.Outbox = /* your MySQL Outbox */;
        opt.ConnectionProvider = typeof(MySqlConnectionProvider);
        opt.TransactionProvider = typeof(MySqlTransactionProvider);

        opt.DistributedLock = new MySqlLockingProvider(new MySqlConnectionProvider(configuration));
    })
    .UseOutboxSweeper(opt => { opt.BatchSize = 10; });
```

## Provisioning

None. MySQL named locks are held in the server and scoped to the session that acquires them, so the provider needs no table or schema. The lock is released when the session ends — including if the holding instance crashes and its connection drops.

## Notes

* MySQL limits lock names to 64 characters, so the provider hashes the resource name (SHA-512, truncated) to produce a valid, stable lock name. You do not need to do anything for this.
* Acquisition uses a one-second timeout: if another instance holds the lock, the attempt returns without acquiring and the Sweeper/Archiver abandons that cycle.
* Because the lock is tied to a session rather than a timed lease, there is no `LeaseValidity` to tune.

## Further Reading

* [Distributed Lock](/paramore-brighter-documentation/outbox-and-inbox/distributedlock.md) — concepts and the full provider list
* [MySQL Outbox](/paramore-brighter-documentation/outbox-and-inbox/mysqloutbox.md) — the matching Outbox
* [Outbox Support](/paramore-brighter-documentation/outbox-and-inbox/brighteroutboxsupport.md) — the Sweeper and Archiver


---

# 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, and the optional `goal` query parameter:

```
GET https://brightercommand.gitbook.io/paramore-brighter-documentation/outbox-and-inbox/distributedlock/mysqldistributedlock.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
