Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is that typical "lock something that is not there" problem. I faced it already several times and still do not have a simple solution for it.</p> <p>This are the options I know until now:</p> <ul> <li>Optimistic: have a unique constraint on the name and let one of the sessions throw on commit. Then you try it again. You have to make sure that you don't end in a infinite loop when another error occurs.</li> <li>Pessimistic: When you add a new Tag, you lock the whole Tag table using TSQL.</li> <li>.NET Locking: you synchronize the threads using .NET locks. This only works if you parallel transactions are in the same process.</li> <li>Create Tags using a own session (see bellow)</li> </ul> <p>Example:</p> <pre><code>public static Tag CreateTag(string name) { try { using (ISession session = factors.CreateSession()) { session.BeginTransaction(); Tag existingTag = session.CreateCriteria(typeof(Tag)) /* .... */ if (existingtag != null) return existingTag; { session.Save(new Tag(name)); } session.Transaction.Commit(); } } // catch the unique constraint exception you get catch (WhatEverException ex) { // try again return CreateTag(name); } } </code></pre> <p>This looks simple, but has some problems. You get always a tag, that is either existing or created (and committed immediately). But the tag you get is from another session, so it is detached for your main session. You need to attach it to your session using cascades (which you probably don't want to) or update.</p> <p>Creating tags is not coupled to your main transaction anymore, this was the goal but also means that rolling back your transaction leaves all created tags in the database. In other words: creating tags is not part of your transaction anymore.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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