将 Quartz .NET 与 NHibernate 集成

发布于 2024-10-18 21:56:51 字数 206 浏览 1 评论 0原文

我已经安装了 Quartz .NET 并创建了 Quartz 数据库。我需要用我自己的自定义数据扩展 Quartz 作业存储。例如,当我通过 Quartz API 添加作业时,我需要在同一数据库事务中将附加信息添加到我自己的自定义表中。我知道 Quartz 中有一个名为 JobStoreCMT 的类,但我还没有找到任何简洁的示例来展示如何向 Quartz 提供 NHibernate 创建的事务。

I have installed Quartz .NET and have created the Quartz database. I need to extend the Quartz job store with my own custom data. For example, when I add a job through the Quartz API, I need to add additional information to my own custom tables within the same database transaction. I know there is a class called JobStoreCMT in Quartz, but I have not been able to find any concise examples showing how to provide Quartz with the transaction that NHibernate creates.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶色山野 2024-10-25 21:56:51

我对 JobStoreCMT 的理解是,您使用 .NET 2.0 中引入的 System.Transactions 命名空间中的类来声明所有事务。幸运的是,NHibernate 也能很好地处理这些事务,因此您可以将 Quartz 操作和 NHibernate 操作放在单个 TransactionScope 中,如下所示:

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();
}

上面的代码假设在其他地方声明并初始化了以下变量:

Quartz.IScheduler _scheduler;
NHibernate.ISessionFactory _sessionFactory;

上面的代码示例未打包详细信息,但这是总体思路。

您可能遇到的一个问题是,上述代码将需要分布式事务,因为 Quartz JobStoreCMT 类创建一个数据库连接,而 NHibernate 会话创建另一个数据库连接。尽管(至少对于 SQL Server)可以让两个不同的数据库连接共享同一个事务,但我的理解是,System.Transactions 的当前实现将在打开第二个连接时将事务升级为分布式事务。这将对性能产生重大影响,因为分布式事务比简单事务慢得多。

My understanding of the JobStoreCMT is that you declare all of your transactions using the classes in System.Transactions namespace that was introduced in .NET 2.0. Fortunately NHibernate operates well with these transactions as well, so you would put your Quartz operations and NHibernate operations in a single TransactionScope, like the following:

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 elsewhere the following vars are declared and initialized:

Quartz.IScheduler _scheduler;
NHibernate.ISessionFactory _sessionFactory;

The above code sample is not packed with detail, but this is the general idea.

One problem you may run into is that the above code will require a distributed transaction because the Quartz JobStoreCMT class 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 share the same transaction, my understanding is that the current implementation of System.Transactions will upgraded the transaction to a distributed transaction with the opening of the second connection. This will have a significant effect on performance as a distributed transaction is significantly slower than a simple transaction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文