NHibernate Persistence

Project Hosting | Nuget: NServiceBus.NHibernate (Version: 5.x)
Target NServiceBus Version: 4.x
Standard support for version 4.x of NServiceBus has expired. For more information see our Support Policy.

Uses the NHibernate ORM for persistence.

Supported Persistence Types

Usage

The next stage is to actually tell NServiceBus how to use NHibernate for persistence

Edit
configure.UseNHibernateSubscriptionPersister();
configure.UseNHibernateTimeoutPersister();
configure.UseNHibernateSagaPersister();
configure.UseNHibernateGatewayPersister();

Connection strings

It is possible to pass connection string in the app.config file, as described in the Using configuration convention section.

Customizing the configuration

To customize the NHibernate Configuration object used to bootstrap the persistence mechanism, either provide a ready-made object via code or use convention-based XML configuration. The code-based approach overrides the configuration-based one when both are used.

Passing Configuration in code

To specific configuration on a per-concern basis, use following

Edit
var nhConfiguration = new Configuration
{
    Properties =
    {
        ["dialect"] = "NHibernate.Dialect.MsSql2008Dialect"
    }
};

configure.UseNHibernateSubscriptionPersister(nhConfiguration);
configure.UseNHibernateTimeoutPersister(nhConfiguration, true);
configure.UseNHibernateSagaPersister(nhConfiguration);
configure.UseNHibernateGatewayPersister(nhConfiguration);
Combine both approaches to define a common configuration and override it for one specific concern.

Using configuration convention

NServiceBus then picks up the connection setting from the app.config from connectionStrings and appSettings sections. The convention used for appSettings does not support defining settings specific for a single persistence concern. If this level of granularity is required use a code-based approach.

When using SQL 2012 or later, change the dialect to MsSql2012Dialect. Additional dialects are available in the NHibernate.Dialect namespace, NHibernate documentation.
Edit
<configuration>
  <connectionStrings>
    <add name="NServiceBus/Persistence"
         connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=nservicebus;Integrated Security=True"/>
  </connectionStrings>

  <!-- specify the other needed NHibernate settings like below in appSettings:-->
  <appSettings>
    <!-- dialect is defaulted to MsSql2008Dialect, if needed change accordingly -->
    <add key="NServiceBus/Persistence/NHibernate/dialect" 
         value="NHibernate.Dialect.MsSql2008Dialect" />
    <!-- other optional settings examples -->
    <add key="NServiceBus/Persistence/NHibernate/connection.provider"
         value="NHibernate.Connection.DriverConnectionProvider" />
    <add key="NServiceBus/Persistence/NHibernate/connection.driver_class"
         value="NHibernate.Driver.Sql2008ClientDriver" />
  </appSettings>
</configuration>

Change database schema

The database schema used can be changed by defining the default_schema NHibernate property. See the previous Customizing the configuration section.

Subscription caching

The subscriptions can be cached when using NHibernate. This can improve the performance of publishing events as it is not required to request matching subscriptions from storage.

Publishing is performed on stale data. This is only advised in high volume environments where latency can be a potential issue.
Edit
configure.UseNHibernateSubscriptionPersister(
    cacheExpiration: TimeSpan.FromSeconds(10));

Generating scripts for deployment

To create scripts, for execution in production without using the installers, run an install in a lower environment and then export the SQL structure. This structure can then be migrated to production.

Samples

Related Articles


Last modified