Quartz.NET Integration with NHibernate

I installed Quartz.NET and created the Quartz database. I need to expand the Quartz job repository with my own data. For example, when I add a task through the Quartz API, I need to add additional information to my own user tables as part of a single database transaction. I know there is a JobStoreCMT class in Quartz, but I could not find brief examples showing how to provide Quartz with the transaction generated by NHibernate.

+4
source share
1 answer

My understanding of JobStoreCMT is that you declare all your transactions using the classes in the System.Transactions namespace that was introduced in .NET 2.0. Fortunately, NHibernate handles these transactions well, so you have to put your Quartz operations and NHibernate operations in one TransactionScope, for example:

using (var ts = new TransactionScope()) { // do something with your scheduler here, eg _scheduler.ScheduleJob(someJobDetail, someTrigger); // create an NHibernate session that will be part of the same transaction using (var session = _sessionFactory.OpenSession()) { // perform actions on session here, but do *not* make use of // NHibernate transaction methods, such as ISession.BeginTransaction(), // because the transaction is controlled via the TransactionScope instance. } ts.Complete(); } 

The code above assumes that the following vars are declared and initialized elsewhere:

 Quartz.IScheduler _scheduler; NHibernate.ISessionFactory _sessionFactory; 

The above code example is not filled with details, but it is a general idea.

One of the problems you may encounter is that the above code will require a distributed transaction, because the Quartz class JobStoreCMT creates one database connection and the NHibernate session creates another database connection. Although it is possible (with SQL Server, at least) to have two different database connections sharing the same transaction, I understand that the current implementation of System.Transactions will update the transaction to a distributed transaction with the opening of the second connection. This will have a significant impact on performance, since a distributed transaction is much slower than a simple transaction.

+2
source

Source: https://habr.com/ru/post/1341025/


All Articles