問題タブ [nhibernate-4]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c# - Parallel.ForEach used with NHibernate resulting in SQL Server locks
Firstly, I am not much of an expert in multi-threading and parallel programming.
I am trying to optimize the performance of a legacy application (.Net 4
, NHibernate 2.1
).
**So far, upgrading NHibernate
is not a priority, but is in the pipeline.
Over time, performance has become a nightmare with the growth of data. One item I have seen is a Parallel.ForEach
statement that calls a method that fetches and updates a complex entity(with multiple relationships - propeties & collections).
The piece of code has the following form (simplified for clarity):
SQL Server intermittently reports database lock errors with the following error:
Transaction (Process ID 20) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction
I suspect it is due to some race condition happening leading up to a deadlock, even if ISessions
are separate.
The ICollection<TheClass>
can have up to 1000 items and each with properties and sub-collections that are processed, generating many SELECT
and UPDATE
statements (confirmed using 'NHibernate Profiler')
Is there a better way to handle this in a parallel way, or shall I refactor the code to a traditional loop?
I do know that I can alternatively implement my code using:
- A
foreach
loop in the sameISession
context - With a Stateless Session
- With
Environment.BatchSize
set to a reasonable value
OR
- Using SQL BulkCopy
I have also read quite a bit of good info about SQL Server deadlocks and Parallel.ForEach
being an easy pitfall:
c# - HQLでブール値リテラルを選択するにはどうすればよいですか?
次のように、HQL で int および string リテラルを選択できます。
select 'a string' as StringLiteral, 1 as IntLiteral from Eg.MyClass
しかし、ブール値リテラルを選択する方法が見つかりませんでした。を試しましたがselect true ...
、select new bool true ...
HQL 構文の例外が発生します。
HQLでブール値リテラルを選択する方法はありますか?
回避策 ( ) を見つけましたselect case when (1=1) then true...
が、効率が悪いようです...