---
title: "The database adapters"
description: "How the Memory, Snapshot, SqlClient and Npgsql adapters implement the database: where each keeps the population, how its transactions meet, how a program chooses one, what the SQL adapters check and need, and where the adapters differ."
audience: "Users"
kind: "Explanation"
status: "Current"
version: "main · v3.2 (unreleased)"
slug: "database-adapters"
url: "https://docs.allors.com/allors3/database-adapters/"
---

# The database adapters

> **Status: Current.** [Where this is implemented](#where-this-is-implemented) names the code and the tests.

The database of an application is an `IDatabase`, and a database adapter implements it. The domain, its rules and the programs that use them call the same API on every adapter: they create transactions, create and change objects, run extents, derive and commit. The adapter decides where the population lives, how transactions see each other’s work, and what a program has to set up before its first transaction. [Terminology](https://docs.allors.com/allors3/terminology.md#adapters) tells these adapters apart from those of the workspace.

<a id="four-adapters"></a>

## Four adapters

| Adapter | Keeps the population | Transactions | Chosen by |
| --- | --- | --- | --- |
| Memory | In the memory of the process, until the process ends. | One: `CreateTransaction()` returns the same transaction until `Init()` or a load replaces it. | The code that creates it, as the domain tests of the sample do. |
| Snapshot | In the memory of the process, until the process ends. | Many, also on several threads. Each reads a snapshot of what was committed. | The setting `adapter`: `snapshot`. |
| SqlClient | In a database of SQL Server. | Many, also in several processes. Each is a snapshot transaction of the server. | The setting `adapter`: `sqlclient`, with a connection string. |
| Npgsql | In a database of PostgreSQL. | As on SqlClient. | The setting `adapter`: `npgsql`, with a connection string. |

[A transaction holds the work](https://docs.allors.com/allors3/objects-and-relations.md#a-transaction-holds-the-work) says when a transaction of each adapter takes its view of the database, and when a commit fails because another transaction changed the same object.

<a id="memory"></a>

### Memory

The Memory adapter keeps one transaction over one population. Nothing else can commit in between, so no commit fails on a conflict, and nothing has to be installed or started. That suits the domain tests: the tests of a rule, an extent or the setup, which build a population and look at it. The domain tests of the sample, and those of Core, run on it unless they are told otherwise.

A program cannot run on it: the setting `adapter` does not offer it. A program that asks for `memory` stops with a message that names the three adapters that it can use, and `DatabaseBuilder.Build()`, called directly, throws a `NotImplementedException`.

<a id="snapshot"></a>

### Snapshot

The Snapshot adapter also keeps the population in memory, but transactions share it, as they share a database on a server. A transaction works on the snapshot that was current when it started, or when it last committed or rolled back, and keeps its changes to itself until it commits. Commits run one at a time, and each publishes a new snapshot. A commit that changes or deletes an object that another transaction changed or deleted after this one read it fails with a `ConcurrencyException`, a `DbException` as the conflict of a server is. A change to the other end of a relation is reconciled instead.

That makes it the adapter for the tests of programs, which use several transactions at once: the tests of the sample’s command-line tool, server and workspace, and the Core and TypeScript suites of the platform. A conflict on an object that two transactions change ends there as it does on a server; [Where the adapters differ](#where-the-adapters-differ) names the concurrent transactions that end otherwise. Its population ends with the process: each run of the command-line tool on the Snapshot adapter starts with an empty database, as [The command-line tool](https://docs.allors.com/allors3/command-line-tool.md#adapters) describes.

<a id="sqlclient-and-npgsql"></a>

### SqlClient and Npgsql

The SqlClient adapter for SQL Server and the Npgsql adapter for PostgreSQL, together the SQL adapters, keep the population in a database of the server, where it outlives every process. A server and the command-line tool of an application reach the same database through the same connection string. Each transaction is a snapshot transaction of the server, on a connection of its own.

The adapters create the tables of the population themselves, from the model, in the schema `allors` of the database. They name the tables, the columns and the indexes after the ids of the model, and they own every table whose name starts with `allors_`. [The database schema](https://docs.allors.com/allors3/database-schema.md) lists what they create.

<a id="before-the-first-transaction-on-a-sql-adapter"></a>

## Before the first transaction on a SQL adapter

Before its first transaction, a SQL adapter checks that the database can store the population as the model describes it. It reports missing or obsolete tables, differences in columns and keys, and changes made by hand that affect reads or writes, such as a trigger on a table of the model. On SQL Server the database must also allow snapshot isolation, and the login must be able to see security policies. Once the schema passes, the adapter checks for missing indexes. [Before the first transaction](https://docs.allors.com/allors3/database-schema.md#before-the-first-transaction) lists the checks and their messages.

To keep the population, correct the problem named in the message. If only indexes are missing, the command `index` creates them and preserves the rows. After a refusal, the same database object repeats the failed check on its next call to `CreateTransaction()`. Once a check passes, it keeps that result for later transactions.

`Init()` empties the population, and a load replaces it with the objects and relations from a file. When a new version of the model needs different tables, [Upgrade a population](https://docs.allors.com/allors3/upgrade-a-population.md) describes how to move the population into them.

The adapters create tables and indexes, but not the database. The command `init` of the command-line tool drops and creates it through an [admin connection](https://docs.allors.com/allors3/configuration.md#admin-connections), and [Host a server](https://docs.allors.com/allors3/host-a-server.md#2-create-the-database-and-a-user) shows the steps.

<a id="how-a-program-chooses-its-adapter"></a>

## How a program chooses its adapter

A program reads the setting `adapter` and the connection string `ConnectionStrings:DefaultConnection` of its [configuration](https://docs.allors.com/allors3/configuration.md#settings) and builds its database with `DatabaseBuilder`. The program stops, with a message that names the setting, when `adapter` has no value or names none of the three adapters, and when a SQL adapter has no connection string. The same program then runs on the Snapshot adapter in a test and on SQL Server or PostgreSQL in production.

`DatabaseBuilder` also takes the command timeout of the SQL adapters, in seconds: the time that a statement of a transaction may take before it fails. The sample’s command-line tool passes its option `-t`, the Core test server passes 60 seconds, and the sample’s server passes none: the timeout of the connection string then applies, 30 seconds unless the connection string sets `Command Timeout`. A program that creates its database in code, without `DatabaseBuilder`, sets these choices on the `Configuration` of the adapter, among them the name of the schema.

The domain tests of the sample create their database themselves: on the Memory adapter, or on the adapter that the variable `ALLORS_TEST_ADAPTER` names, without reading the configuration of the application. [Test on PostgreSQL or SQL Server](https://docs.allors.com/allors3/test-on-postgresql-or-sql-server.md) shows how.

<a id="where-the-adapters-differ"></a>

## Where the adapters differ

The adapters implement one API, and most of the shared tests of the platform run on all four. Where an adapter departs from the others, a test on the Memory or the Snapshot adapter does not show what SQL Server or PostgreSQL will do:

-   **Sorting.** The adapters put the objects that have nothing in the role of a sort in different places; [Sort](https://docs.allors.com/allors3/extents.md#sort) says where.
-   **Filters.** `AddLike` with an empty pattern, a backslash or a value of several lines differs between the adapters, as [Filter](https://docs.allors.com/allors3/extents.md#filter) says. On SQL Server, with a database collation that ignores case, both `AddEquals(role, "ABRA")` and `AddLike(role, "ABRA")` match a stored `"abra"`. For a stored `"abc "` with a trailing space, `AddEquals(role, "abc")` matches and `AddLike(role, "abc")` returns no match. Issue [#405](https://github.com/allors/allors3/issues/405) gives the details.
-   **Combined extents.** The Memory and the Snapshot adapters refuse an extent that is already the operand of another operation; [Combine extents](https://docs.allors.com/allors3/extents.md#combine-extents) gives the advice.
-   **Change sets.** A binary role that gets another array with the same bytes is a change on the Memory and the SQL adapters, and none on the Snapshot adapter, as [Change sets](https://docs.allors.com/allors3/objects-and-relations.md#change-sets) says.
-   **Concurrent transactions.** The Snapshot adapter reconciles the two ends of a relation when it commits, where SQL Server and PostgreSQL see only the rows that the transactions write. Two transactions that give one object to two associations where its role allows one, or that relate an object that the other deletes, can both commit on a server, which then holds both relations or a relation to the deleted object. When the relation is a column of the row of the role, as for a one-to-many relation, the second commit fails instead, even when the first only changed a unit role of that object, and the Snapshot adapter commits both. Issue [#341](https://github.com/allors/allors3/issues/341) lists the cases.
-   **Loading.** When a file holds a role with two associations where the model allows one, the adapters keep different associations; [A file of an earlier model](https://docs.allors.com/allors3/populations.md#a-file-of-an-earlier-model) and issue [#339](https://github.com/allors/allors3/issues/339) say more.
-   **Indexes.** Only the SQL adapters create indexes, so `[Indexed]` changes nothing on the Memory and the Snapshot adapters.

The SQL adapters also have limits that the Memory and the Snapshot adapters do not share. Open issues record them, among them how many statements deleting an object takes ([#386](https://github.com/allors/allors3/issues/386)), whether a role or an association that holds many objects is loaded whole to answer an existence check ([#433](https://github.com/allors/allors3/issues/433)), and what a commit after a failed flush stores ([#412](https://github.com/allors/allors3/issues/412)).

To check a rule, an extent or a setup on the adapter of production, run the domain tests on it, as [Test on PostgreSQL or SQL Server](https://docs.allors.com/allors3/test-on-postgresql-or-sql-server.md) shows.

<a id="where-this-is-implemented"></a>

## Where this is implemented

-   `IDatabase` and `ITransaction` are in `dotnet/System/Database/Allors.Database`. The adapters are in `dotnet/System/Database/Adapters`: `Allors.Database.Adapters.Memory`, `Allors.Database.Adapters.Snapshot`, `Allors.Database.Adapters.Sql` with what the two SQL adapters share, `Allors.Database.Adapters.Sql.SqlClient` and `Allors.Database.Adapters.Sql.Npgsql`. `Allors.Database.Adapters` holds what all four share, such as the checks of the values of unit roles.
-   `DatabaseBuilder`, in `dotnet/System/Database/Adapters/Allors.Database.Builder`, builds the database that the setting `adapter` names. `DatabaseBuilderTests`, in `dotnet/Core/Database/Server.Local.Tests`, checks its messages for a missing or unknown adapter and a missing connection string.
-   The shared adapter tests are in `dotnet/System/Database/Adapters/Tests.Static/Static`, and the folders `Memory`, `Snapshot`, `Sql/SqlClient` and `Sql/Npgsql` next to them run them on each adapter: `ConcurrencyTest` on all but the Memory adapter, and `CacheTest` and `SchemaTest` on the SQL adapters only. [Build and test](https://docs.allors.com/allors3/internals/build-and-test.md#run-a-suite) names the suite of each adapter.
