Note that there are some explanatory texts on larger screens.

plurals
  1. PONhibernate: Why does this cause a deadlock?
    primarykey
    data
    text
    <p>I only have a single resource that SQL Server should be locking which is the Company table but the following console application is deadlocking. Why?</p> <pre><code>using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using DataAccess; using NHibernate; using NHibernate.Cfg; using NHibernate.Context; using NHibernate.Tool.hbm2ddl; namespace NhibernatePlayground { class Program { private static ISessionFactory _sessionFactory; private static Configuration _configuration; private static int TotalThreadCount = 20; private static int LoopCount = 1000; private static void Main(string[] args) { //App_Start.NHibernateProfilerBootstrapper.PreStart(); _configuration = BuildConfiguration(); var se = new SchemaExport(_configuration); se.Create(false, true); _sessionFactory = _configuration.BuildSessionFactory(); int companyId = Seed(); Stopwatch sw = new Stopwatch(); sw.Start(); Task[] tasks = new Task[TotalThreadCount]; for (int i = 0; i &lt; TotalThreadCount; i ++) { tasks[i] = Task.Factory.StartNew(() =&gt; IncreaseEmployeeCount(LoopCount, companyId)); } //Block until all tasks complete. Task.WaitAll(tasks); sw.Stop(); Console.WriteLine("Employee Count: " + GetEmployeeCount(companyId)); Console.WriteLine("Total Milliseconds: " + sw.ElapsedMilliseconds); Console.ReadKey(); } private static Configuration BuildConfiguration() { Configuration configuration = new Configuration(); configuration.Configure(); // A configuration.AddAssembly(typeof(Company).Assembly); // B return configuration; } private static void IncreaseEmployeeCount(int count, int companyId) { for (int i = 0; i &lt; count; i++) { using (ISession _session = _sessionFactory.OpenSession()) { using (ITransaction _transaction = _session.BeginTransaction(IsolationLevel.Serializable)) { var company = _session.Get&lt;Company&gt;(companyId); company.EmployeeCount++; _session.Save(company); _transaction.Commit(); } } } } private static int Seed() { using (ISession _session = _sessionFactory.OpenSession()) { using (ITransaction _transaction = _session.BeginTransaction()) { Company company = new Company { CompanyName = "Angus" }; _session.Save(company); _transaction.Commit(); return company.Id; } } } private static int GetEmployeeCount(int companyId) { using (ISession _session = _sessionFactory.OpenSession()) { using (ITransaction _transaction = _session.BeginTransaction()) { Company company = _session.Get&lt;Company&gt;(companyId); return company.EmployeeCount; } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload