Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm testing using sqlite. I have a different approach.</p> <p>I let NH create the connection, but store it in a static variable to pass it to subsequent sessions.</p> <p>When I begun to write the test framework, I had initialization code like this:</p> <pre><code>static IDbConnection staticSqliteConnection; if (staticSqlliteConnection == null) { sessionFactory.CreateSession().Connection; } </code></pre> <p>Then I had some issues and had too much test specific code in my production code (I prefer to have none). Hi-Lo id generators do also not work if you pass the connection to the session.</p> <p>Then I wrote a ConnectionProvider class looking like this:</p> <pre><code>public class SingleConnectionProvider : DriverConnectionProvider { private static IDbConnection staticConnection = null; public static void ResetConnection() { if (staticConnection == null) return; staticConnection.Dispose(); staticConnection = null; } public override IDbConnection GetConnection() { if (staticConnection == null) { staticConnection = base.GetConnection(); } if (staticConnection.State != ConnectionState.Open) { staticConnection.Open(); } return staticConnection; } protected override void Dispose(bool isDisposing) { ResetConnection(); } } </code></pre> <p>In the unit test cleanup method, you call <code>SingleConnectionProvider.ResetConnection()</code> which destroys the database for the next test.</p> <p>The whole thing is of course not fully thread safe. You can create as many sessions as you want, but you should avoid to create them concurrently.</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. VO
      singulars
      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