> 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/mssqldistributedlock.md).

# MSSQL Distributed Lock

The MS SQL locking provider implements Brighter's [distributed lock](/paramore-brighter-documentation/outbox-and-inbox/distributedlock.md) using SQL Server **application locks** (`sp_getapplock` / `sp_releaseapplock`), 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 [MSSQL Outbox](/paramore-brighter-documentation/outbox-and-inbox/mssqloutbox.md).

## Package

* **Paramore.Brighter.Locking.MsSql**

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

## Configuration

Unlike the lease-based providers, the MS SQL provider does not take an options class. You construct it with an `MsSqlConnectionProvider`, which it uses to open the session that holds the application 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;Trusted_Connection=True;");

var lockingProvider = new MsSqlLockingProvider(new MsSqlConnectionProvider(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;Trusted_Connection=True;");

services
    .AddBrighter()
    .AddProducers(opt =>
    {
        opt.Outbox = /* your MS SQL Outbox */;
        opt.ConnectionProvider = typeof(MsSqlConnectionProvider);
        opt.TransactionProvider = typeof(MsSqlTransactionProvider);

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

## Provisioning

None. SQL Server application locks live in the server and are scoped to the session that acquires them, so the provider needs no table or schema. The lock acquires in `Exclusive` mode at `Session` scope and is released when the session ends — including if the holding instance crashes and its connection drops.

## Notes

* Because the lock is tied to a session rather than a timed lease, there is no `LeaseValidity` to tune.
* Point the provider at the same SQL Server instance as your Outbox so the Sweeper, Archiver, and lock share infrastructure.

## Further Reading

* [Distributed Lock](/paramore-brighter-documentation/outbox-and-inbox/distributedlock.md) — concepts and the full provider list
* [MSSQL Outbox](/paramore-brighter-documentation/outbox-and-inbox/mssqloutbox.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/mssqldistributedlock.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.
