SQLCLR 触发器与 Windows 服务。什么时候适合使用SQLCLR?

发布于 2024-09-28 15:24:10 字数 685 浏览 1 评论 0原文

我们有一个带有 SQL Server 2005 后端的 .NET 电子商务应用程序。新订单需要一定的“后处理”。这些任务包括发送电子邮件、创建文件、将文件上传到 FTP 服务器以及针对 WCF 数据服务执行 CRUD 操作。执行所有这些任务的代码已作为多个 .NET 类库就位。

我的团队正在进行的争论是将该代码放置在哪里。我编写了一个简单的 Windows 服务,它定期轮询数据库,并在检测到数据库中的新事务(基于标志)时,它会执行必要的操作并记录任何错误。已提出的替代方案是 SQLCLR INSERT 触发器,它将启动处理。

我知道在 SQLCLR 中完成上述大部分(全部?)任务在技术上是可行的 — 我什至发现了许多文章解释如何从 SQLCLR 中使用 Web 服务,所以显然人们正在这样做。但我还在犹豫。 SQLCLR 曾经用于这种事情吗?如果不是,实际的缺点是什么?至于 SQLCLR 触发器相对于 Windows 服务的潜在好处,我只能看到一个:更少的数据库流量。我们预计最初​​的交易量非常小,因此 Windows 服务将产生一些“浪费”的流量。但该服务与数据库位于同一台机器上,因此它甚至不会影响网络,只会影响内部服务器资源。

最后,第三种可能性是使用 SQLCLR 触发器在文件系统上保存一个简单的令牌,并使用 Windows 服务中的 FileSystemWatcher(而不是 Timer)来根据需要执行任务。

请分享您对这些不同方法的权衡的想法或提出更好的替代方案。

We have a .NET e-commerce application with a SQL Server 2005 back-end. A new order requires certain "post-processing". These tasks include sending emails, creating files, uploading files to an FTP server, and performing CRUD operations against a WCF data service. The code to perform all these tasks is already in place as several .NET class libraries.

The debate that my team is having is where to place this code. I've written a simple Windows service that polls the db at regular intervals and upon detecting new transactions (based on flags) in the db, it does the necessary thing and logs any errors. An alternative that has been proposed is a SQLCLR INSERT trigger that will set off the processing.

I know it's technically possible to do much (all?) of the above tasks in SQLCLR -- I've even found a number of articles explaining how to consume web services from within SQLCLR, so apparently people are doing this. But I am still hesitant. Was SQLCLR ever intended for this kind of thing? And if not, what could be the practical downside? As for the potential benefits of a SQLCLR trigger over a windows service, I can only see one: less db traffic. We expect very light transaction volume initially, so the windows service will generate some "wasteful" traffic. But the service is on the same box as the database, so it doesn't even affect the network, only internal server resources.

Finally, a third possibility would be to use a SQLCLR trigger to save a simple token on the file system, and use FileSystemWatcher in the Windows service (instead of Timer) to perform the tasks as needed.

Please share your thoughts on the trade-offs of these different approaches or suggest a better alternative.

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

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

发布评论

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

评论(2

落叶缤纷 2024-10-05 15:24:10

在查看类似的流程时,我们考虑了一些事情。

  1. 当依赖系统(邮件、FTP)不可用时会发生什么?
  2. 故障恢复如何进行?
  3. 如果我们处于失败状态,新记录会发生什么情况。即使我们知道系统已关闭,他们是否也会尝试进行后期处理?
  4. 支持生产系统的人员具备哪些技能?
  5. 如何监控这个过程?
  6. 后处理系统在处理时是否消耗主系统的资源?你会如何将它们分开?
  7. 是否支持故障转移?

我们选择使用 Windows 服务,因为它为那些支持系统的人(那些愿意花更多时间在服务器故障而不是 stackoverflow 上的人)提供了最佳体验。因为它有一个既定的方法来停止和启动服务、监控、集群支持、轻松地将处理拆分到多台机器、与事件日志集成等。

When looking at a similar process here were some things we considered.

  1. What happens when a dependant system (mail, ftp) isn't available?
  2. How does the recovery of a failure work?
  3. If we're in a fail state what happens to new records. Do they try and do the post process even when we know the system is down?
  4. What is the skill set of those who are supporting the production system?
  5. How is this process monitored?
  6. Is the post processing system consuming the resources of the main system while processing? How would do you separate them?
  7. Is fail-over supported?

We choose to use a Windows Service because it offered the best experience to those that would be supporting the system (people who would spend more time on serverfault instead of stackoverflow). Since it had an established way to stop and start the service, monitoring, cluster support, easy to split processing to multiple machines, integration with event logging, etc.

青巷忧颜 2024-10-05 15:24:10

SQL CLR 是为某些事情而设计的 - 但这不是其中之一,将此类内容放入 INSERT 触发器中确实会让小耶稣哭泣。

一个潜在的解决方案是使用查询通知来减轻服务和数据库之间的负载 - 可以采取另一种方法,允许通过 UDP 或类似的轻量级方式“ping”服务 - 这意味着您“不会导致 INSERTS 上发生大量事件(因此您不会长时间锁定基础表)。

There are some things that the SQL CLR was designed for - and this isn't one of them, putting that sort of stuff in an INSERT trigger really would make baby Jesus cry.

One potential solution would be to use Query Notification in order to lighten the load between the service and the database - another route could be taken by allowing the service to be 'pinged' via UDP or something similarly light-weight - meaning that you aren't causing masses to happen on INSERTS (and you are therefore not locking the underlying table for excessive amounts of time).

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