I've been working on a system with a large and complex entity model (using 3 sessionfactories/databases etc.) that is used with a web application. I have now reached a point where I need to implement a number of "long running" batch processes, and am wondering what the best approach is. These batch processes involve iterating over a lot of records and traversing many entity associations while producing an output - one instance is producing letters to be printed, the other is carrying out calculations to produce invoices and invoice lines, and there are others as well
Each individual action as part of the batch (i.e. each iteration) is not too long, but the entire batch process could be - up to a number of minutes. I'm concerned about locking of tables impacting on web users.
The entire process doesn't need to be transactional (although in the case of invoicing, this would be nice).
Is the best approach to create a session at the start of the batch process, and then put each iteration of the loop into it's own transaction, beginning and committing the transaction at the start and end of the loop, using the same session for the whole batch? Or should I also open and close the session at the start and end of each iteration? (based on what I've read in the NHibernate manual on page 154, below)
Any advice would be appreciated.
2 of the 3 batch processes I'm writing could be contained to overnight processing, where locking is not such an issue, but the third runs numerous times throughout the day, and could impact web users.
Would another option be detached objects? This would not be preferable, gievn the potential size of the data needed to process the entire batch
Quote:
In a two tiered architecture, consider using session disconnection.
Database Transactions have to be as short as possible for best scalability. However, it is often neccessary to
implement long running Application Transactions, a single unit-of-work from the point of view of a user.
This Application Transaction might span several client requests and response cycles. Either use Detached
Objects or, in two tiered architectures, simply disconnect the NHibernate Session from the ADO.NET connection
and reconnect it for each subsequent request. Never use a single Session for more than one Application
Transaction usecase, otherwise, you will run into stale data.