开发者

Is using transaction with each NHibernate operation necessary in order to using caching?

Is using transaction with each NHibernate operation necessary in 开发者_运维技巧order to using caching and why?


If you do not use an explicit transaction. Most databases will use implicit transactions. This means that each query you make will be wrapped in a transaction that is committed upon query completion. See this article: Use Of Implicit Transactions Is Discouraged

So it is a good idea to wrap your application actions in transactions, even if all they do is fetch data. Especially since your question is dealing with caching, you want to use transactions if you want to make use of the 2nd level cache as per the article referenced.


In database systems, transactions are intended to wrap several smaller atomic operations into one larger operation.

The canonical example is that of moving money from one checking account to another. The two atomic operations are:

  1. Debit $x.xx from one account
  2. Credit $x.xx to another account

These two atomic operations are wrapped in a transaction so that, if one of the operations fails, you can roll back the entire transaction, and the system won't be left in a state where the bank or the customer is left with too much or too little money, or there is money unaccounted for.

So if your operation is a simple atomic one, like changing a single field in a table, then no, I do not believe you need a transaction for that.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜