将 Quartz .NET 与 NHibernate 集成
我已经安装了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 JobStoreCMT 的理解是,您使用 .NET 2.0 中引入的 System.Transactions 命名空间中的类来声明所有事务。幸运的是,NHibernate 也能很好地处理这些事务,因此您可以将 Quartz 操作和 NHibernate 操作放在单个 TransactionScope 中,如下所示:
上面的代码假设在其他地方声明并初始化了以下变量:
上面的代码示例未打包详细信息,但这是总体思路。
您可能遇到的一个问题是,上述代码将需要分布式事务,因为 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:
The code above assumes that elsewhere the following vars are declared and initialized:
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.