开发者

How to manage session/transaction lifetime for proccessing many entities

开发者 https://www.devze.com 2023-02-16 08:04 出处:网络
In the project my team is working on, there is a windows service which iterates through all the entities in a certain table, and updates some of their fields based on some rules we defined. We use NHi

In the project my team is working on, there is a windows service which iterates through all the entities in a certain table, and updates some of their fields based on some rules we defined. We use NHibernate as our ORM tool. Currently, we open one session and one transaction for the entire proccess, which means the transaction is commited after all the entities have been proccessed. I think this approach isn't good, and I wanted to hear some more opini开发者_StackOverflowos: Should we keep our current way of managing the session, Or should move to a different approach?

One option I thought about is opening a transaction per entity, and another suggestion was to open a new session for each entity. What approach you think will work best?


There isn't a single way to do it; it all depends on the specific cases.

In the app I'm working on, I have examples of the three approaches, and there's a reason for choosing each one. For example:

  • The whole process must have transactional atomicity: use a single transaction
  • The process has a lot of common data, but each record in the "master" table can be considered a unit of work: use a single session, multiple transactions
  • Processing each record in the master table should be independent from the others (including error handling): use a session per record
0

精彩评论

暂无评论...
验证码 换一张
取 消