Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Database structure

In Link 3 all data is divided into two primary Databasesdatabases:

  • Configuration

  • Transactional

...

Although these entities are related across databases, there are NO database constraints enforcing the data consistency.

Transactions

In many cases when updating multiple entities it makes sense to join these updates in a database transaction. This gives you the possibility to roll back ALL changes if one of them fails.

To make things easier Link 3 provides a factory called ITransactionScopeFactory that can be dependency injected into your class.

Example: Using transaction scopes

In the example below we update multiple entities in the Configuration database. If an exception occurs the changes are automatically rolled back.

Code Block
languagec#
using (var scope = transactionScopeFactory.CreateScope())
{
    try
    {
        //Updates to configuration data
        scope.Complete();
    }
    catch (Exception ex)
    {
        logger.LogError(ex, $"Exception occured: {ex.Message}");
    }
}

Example: Using transaction scopes across multiple databases.

Transactions that joins consists of updates in to multiple databases are called distributed transactions. These Unfortunately these types of transactions are not supported in Linux and would result in a PlatformException PlatformNotSupportedException.

However it is possible to “break out” of a scope by using the method “RunOutsideTransaction” as seen in line 7 in the example below:

Code Block
languagec#
using (var scope = transactionScopeFactory.CreateScope())
{
    try
    {
        //Updates to configuration data

        await transactionScopeFactory.RunOutsideTransaction(() =>
        {
            //Updates to transactional data
        });

        scope.Complete();
    }
    catch (Exception ex)
    {
        logger.LogError(ex, $"Exception occured: {ex.Message}");
    }
}