How is the Unit of Work used with batch processing?

前端 未结 2 1538
慢半拍i
慢半拍i 2021-01-24 15:03

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

2条回答
  •  没有蜡笔的小新
    2021-01-24 15:29

    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.

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

    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.

    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. 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.

    For bulk read operations, IStatelessSession is better solution.

提交回复
热议问题