Should I always use transactions in nhibernate (even for simple reads and writes)?

后端 未结 1 1431
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 14:50

I know that for multi part writes, I should be using transactions in nhibernate. However what about for simple read and writes (1 part) ... I\'ve read that it\'s good practi

相关标签:
1条回答
  • 2020-12-17 15:46

    Best recommendation would be to always use a transaction. This link from the NHProf documentation, best explains why.

    When we don't define our own transactions, it falls back into implicit transaction mode, where every statement to the database runs in its own transaction, resulting in a large performance cost (database time to build and tear down transactions), and reduced consistency.

    Even if we are only reading data, we should use a transaction, because using transactions ensures that we get consistent results from the database. NHibernate assumes that all access to the database is done under a transaction, and strongly discourages any use of the session without a transaction.

    (BTW, if you are doing serious NHibernate work, consider trying out NHProf).

    0 讨论(0)
提交回复
热议问题