|
| 1 | +--- |
| 2 | +description: Describes the checks Umbraco will do on startup to determine the availability of the database, and how this behavior can be customized. |
| 3 | +--- |
| 4 | + |
| 5 | +# Database Availability Checks |
| 6 | + |
| 7 | +When Umbraco boots it will check for a configured database and, if found, verify that a connection can be made. |
| 8 | + |
| 9 | +The default behavior is to check five times with a one second delay between attempts. If, after that, a connection cannot be established, Umbraco will fail with a `BootFailedException`. |
| 10 | + |
| 11 | +## Implementing Custom Behavior |
| 12 | + |
| 13 | +For projects in development with the potential for misconfigured database settings, this is likely a reasonable approach to take. |
| 14 | + |
| 15 | +In production, when you have stable configuration, you may prefer to override the behavior to better handle cases where your hosting infrastructure might restart. |
| 16 | + |
| 17 | +We support this by abstracting the default behavior behind the `IDatabaseAvailabilityCheck` interface found in the `Umbraco.Cms.Infrastructure.Persistence` namespace. |
| 18 | + |
| 19 | +You can implement your own version of this interface and register it via a composer. This is shown in the following, stub example: |
| 20 | + |
| 21 | +```csharp |
| 22 | +using Umbraco.Cms.Core.Composing; |
| 23 | +using Umbraco.Cms.Infrastructure.Persistence; |
| 24 | + |
| 25 | +public class MyDatabaseAvailabilityCheckComposer : IComposer |
| 26 | +{ |
| 27 | + public void Compose(IUmbracoBuilder builder) |
| 28 | + { |
| 29 | + builder.Services.AddUnique<IDatabaseAvailabilityCheck, MyDatabaseAvailabilityCheck>(); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +internal class MyDatabaseAvailabilityCheck : IDatabaseAvailabilityCheck |
| 34 | +{ |
| 35 | + public bool IsDatabaseAvailable(IUmbracoDatabaseFactory databaseFactory) |
| 36 | + { |
| 37 | + // Provide your custom logic to check database availability, wait as required, and return true once a connection is established. |
| 38 | + return true; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +``` |
| 43 | + |
| 44 | +For reference and inspiration, the default implementation can be found [here](https://github.com/umbraco/Umbraco-CMS/blob/main/src/Umbraco.Infrastructure/Persistence/DefaultDatabaseAvailabilityCheck.cs). |
| 45 | + |
0 commit comments