Skip to content

main · v3.2 (unreleased)

The database adapters

Users · Explanation · CurrentMarkdownAgent index

Status: Current. 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 tells these adapters apart from those of the workspace.

AdapterKeeps the populationTransactionsChosen by
MemoryIn 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.
SnapshotIn 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.
SqlClientIn 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.
NpgsqlIn a database of PostgreSQL.As on SqlClient.The setting adapter: npgsql, with a connection string.

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.

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.

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 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 describes.

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 lists what they create.

Before the first transaction on a SQL adapter

Section titled “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 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 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, and Host a server shows the steps.

A program reads the setting adapter and the connection string ConnectionStrings:DefaultConnection of its configuration 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 shows how.

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 says where.
  • Filters. AddLike with an empty pattern, a backslash or a value of several lines differs between the adapters, as 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 gives the details.
  • Combined extents. The Memory and the Snapshot adapters refuse an extent that is already the operand of another operation; 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 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 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 and issue #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), whether a role or an association that holds many objects is loaded whole to answer an existence check (#433), and what a commit after a failed flush stores (#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 shows.

  • 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 names the suite of each adapter.