Why NH encourage user to using transaction #2999
-
I have a story that my friend said NH encourage using transaction. and list his point in that NH official document
On his website which has more than 1,000,000 access per day. In his code, he created a new transaction at the start of every APIs and commit it in the outlet of API. It is a tragedy that website only support a few concurrences. So, I don't understand, Why NH encourage users to use transaction? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 14 replies
-
That is not an "NH official document". That is the web page of a third party product. |
Beta Was this translation helpful? Give feedback.
-
I think that article does a fairly good job of explaining why you should use transactions and that in fact you are anyway using transactions even if you don't create them yourself. Not creating the transaction yourself generally leads to MORE transactions, so combining all work for a batch in a single transaction generally gives better performance and more robust logic, both for reading and writing. This is therefore the safer choice generally.
If you feel that your website is slow because of transactions I suspect you have some other problem. You can have thousands of transactions reading the same data concurrently. They would only be blocked by writers. And only if the same data is affected. If you have hundreds of transactions trying to READ some data that is also being written, why do you want the readers to possibly get partially written data? And btw if a long-running transaction has taken a write-lock on some data, a SELECT statement for that data might be blocked even if you didn't create a transaction yourself for the read call. Some things to consider:
|
Beta Was this translation helpful? Give feedback.
-
Oskarb has already said most there is to say about it. Here is an additional NHibernate specific reason: when not using explicit transactions, the second level cache cease to work as soon as some data has been written. (At least for the entities/spaces touched by the data change.) So an application cannot benefit from the second level cache without explicit transactions. And here is a personal experience case: I had been given a Web site to handle, which was using NHibernate. There was no transaction handling in it. At some point I have put one in place, with a one transaction per request pattern. It had at first a bad impact, because I forgot to check the concurrency level. Once I fixed that (read committed snapshot indeed), there was no more any bad impact. Quite the contrary: it did fix concurrency bugs occurring on some cases, and improved performances because the second level cache (which was in place from the beginning) could then work properly.
Are these operations an actual unit of work? Should the user update be done only if the order is guaranteed to also be inserted? If yes, rearrange the code to group together these two writes in a single short explicit transaction without logic in between, instead of locking the user data for half a second, with is a huge duration for a system needing to support concurrency. If no, use distinct explicit transactions, without forgetting to include in them all data reads which would be required by the logic to know what update to do, of course. By the way, NHibernate documentation is here, and it does advise against not using explicit transactions here:
|
Beta Was this translation helpful? Give feedback.
-
The disadvantage of using transaction
Advantage of using transaction
We should know the advantage and disadvantages of transaction I list above, If you don't agree above, It should be another discussion topic. So you underestimate the destructiveness to recommend using explicit transaction cross whole application. The more store: The developer(architect) who wrote code at BeginRequest to create a new transaction with default isolation level(read_commit) protected void Application_BeginRequest()
{
environment.Resolve<ITransactionManager>().RequireNew();
// ...
} There are more than 10 developers who wrote more than 500 APIs cross 2 years. everything and code work perfectly local and even in testers who just have the regular test (without pressure test, actually they are not aware that). I just ask the developer why do you create a new transaction beginning of API? He answered what you said to enable "second cache level" and NH encourages to create a transaction per request. I made an example before
The second one is I just made a very simple example. The more complex is that APIs impact APIs. even include complex SQL queries. I dug the "second cache level" in NH, It's great. But I don't think NH should recommend users to use transactions in every request or NH should list the rule of using transactions. You another suggest changing the transaction isolation level. It is an option to resolve. But the problem is the isolation level set at the global level. It should be a big problem to think about how much API will be impacted.
@oskarb I agree with what you said. If NH recommends using explicit transaction, you should need to list the rule of using transaction. |
Beta Was this translation helpful? Give feedback.
-
I don't think I can express my thoughts on the technical issues more than I already have, at least I don't have time to. One other thing that does occur to me. You mention "10 developers, 500 API calls, 2 year project". You mentioned "junior developers" somewhere also. I assume that everyone involved in the project has been doing their very best, I have no reason to think otherwise. However, this is a huge project. I don't think blaming/scapegoating some guideline about transactions that someone took from a blog article is very helpful here, to be honest. It would be unfortunate if these developers (and others) come away from this project being told that "transactions are bad for performance, don't use them". This would certainly not be the first or the last project in the history of IT that was surprised by performance issues late in the project. More useful lessons would be to consider the process that lead to the situation. Should performance tests have been done earlier? Should we and could we have analysed some aspects in more detail? How could we have realised that earlier? Should we have had more in-depth knowledge available in the project team? |
Beta Was this translation helpful? Give feedback.
That is not an "NH official document". That is the web page of a third party product.