Accessing data via NHibernate persistence

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

NHibernate persistence supports a mechanism that allows using the same data context used by NServiceBus internals to also store business data. This ensures atomicity of changes done across multiple handlers and sagas involved in processing of the same message. See accessing data to learn more about other ways of accessing the data in the handlers.

ISession instances can also be injected directly into the handlers.

Edit
public class OrderHandler :
    IHandleMessages<OrderMessage>
{
    ISession session;

    public OrderHandler(ISession session)
    {
        this.session = session;
    }

    public void Handle(OrderMessage message)
    {
        session.Save(new Order());
    }
}

The handler uses constructor injection to access the ISession object.

Regardless of how the ISession object is accessed, it is fully managed by NServiceBus according to the best practices defined by NServiceBus documentation with regards to transactions.

Known limitations

NServiceBus opens a session by passing an existing instance of a database connection. Therefore, it is not possible to use NHibernate's second-level cache. The reason why sessions are opened this way in NServiceBus is because of an unresolved bug in NHibernate.

Related Articles


Last modified