本文介绍了工作单元如何与批处理一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构建Web应用程序时,通常的做法是每个HTTP请求使用一个工作单元,并在处理请求后刷新所有更改并提交一次事务.

When building a web application, it is a standard practice to use one Unit of Work per HTTP request, and flush all the changes and commit the transaction once after handling the request.

构建批处理过程时使用什么方法?是否在整个过程执行过程中使用一个工作单元实例,并在逻辑点上定期提交事务?

What approach is used when building a batch process? Is one Unit of Work instance used for the whole execution of the process, with transactions committed periodically at logical points?

这可能不是实际问题,而是更多的讨论,但我希望找出是否存在类似于每次请求会话"的普遍接受的最佳实践".

This may be more of a discussion than a factual question, but I'm hoping to find out if there is a commonly-accepted "best practice" analogous to Session-per-Request.

推荐答案

工作单位是您的业务交易.它在ISession的范围内定义.它应该更短;但不要太短.因此,建议使用每次请求会话".这样,您就可以利用UoW的各种功能,例如跟踪,一级缓存,自动刷新等.这可以避免数据库的某些往返操作并提高性能.

Unit Of Work is your business transaction. It is defined on scope of ISession. It should be shorter; but not too short. That is why, Session-Per-Request is recommended. With this, you can take advantage of various UoW features like tracking, first level cache, auto flushing etc. This may avoid some round trips to database and improve performance.

ISession范围很短,例如每次操作会话"(根本没有UoW),您会错过上面提到的所有好处.

With very short ISession scope like Session-Per-Operation (No UoW at all), you miss all those benefits mentioned above.

由于不必要地增加了ISession范围(如按应用会话数)或将不相关的操作分组,您会产生许多问题,例如无效的代理状态,增加的内存使用量等.

With unnecessarily increasing ISession scope like Session-Per-Application OR grouping non related operations, you create many problems like invalid proxy state, increased memory usage etc.

考虑到以上,对于批处理,请尝试识别批中较小的UoW.如果您可以将批次拆分为一些小的UoW部件,请继续进行.如果无法拆分批次,则有两种方法:

Considering above, for batch processing, try to identify smaller UoW in your batch. If you can split batch in small UoW parts, go ahead with it. If you cannot split the batch, you have two ways to go:

  1. 整个批次中的单个ISession:
    如果您的批处理一遍又一遍地处理相同的记录,那么这可能会很有用.通过延迟刷新,您将获得一些性能优势.
    即使您的批处理仅处理一次每个记录,这仍可能由于减少刷新和节省数据库往返次数而受益.请参阅下面的第2点.
  2. 批处理中每个操作的新ISession:
    如果您的批处理仅处理每个记录一次,那么可能会更好.我不能肯定地说出完整的方案是未知的.
  1. Single ISession for entire batch:
    If your batch processes same records over and over, then this may be useful. With delayed flushing, you will get some performance benefit.
    Even if your batch processes each record only once, this may still benefit due to reduced flushes and saved round trips to database. Refer point 2 below.
  2. New ISession for each operation in batch:
    If your batch processes each record only once, then this may be better. I cannot say for sure as complete scenario is unknown.

两者都有上述缺点;最好尝试找出批次中较小的单位.

Both have drawbacks mentioned above; better try to find out smaller UoW inside your batch.

对于批量读取操作,IStatelessSession是更好的解决方案.

For bulk read operations, IStatelessSession is better solution.

这篇关于工作单元如何与批处理一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:01
查看更多