Forcing OpenAccess to close the database.

5 posts, 1 answers
  1. Jakkie Esschert van den
    Jakkie Esschert van den avatar
    61 posts
    Member since:
    Sep 2009

    Posted 08 Oct 2010 Link to this post

    Hi,

    I'm trying to use SQL Server CE for unit testing, but ran into an issue when trying to reset the database. I (re)create the database in the setup before each test. To recreate the database I delete the DB file if it exists. That delete fails because the file is locked when two consecutive tests are being run, presumably because there is still an open connection to the database. I think I need to fully 'shut down' OpenAccess so it will release it's open database connections. Is there a proper way to make OpenAccess close it's database connections?

    Aong the same lines, is there a way to make OpenAccess flush it's caches so we can ensure the data we fetched is actually retrieved from the database?

  2. Answer
    TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 08 Oct 2010 Link to this post

    Hi Jakkie,

    How is your test structured? Did you follow the suggestions here?

    In general if you dispose the ObjectScope attached to the unit test and the eventually also the Database instance from the ObjectScope provider, I think it will be possible to replace the database.
    It is the Database instance that actually abstracts the connection to the physical database from OA' perspective.

    Regards

    Henrik
  3. DevCraft banner
  4. Jakkie Esschert van den
    Jakkie Esschert van den avatar
    61 posts
    Member since:
    Sep 2009

    Posted 08 Oct 2010 Link to this post

    I initially ignored that article since I'm using the new OpenAccesContext. I'd did put me in the right direction though, thanks.

    A bit of searching made me find a protected method which allows me to get the ObjectScope. That allowed me to dispose the scope and the attached database. Now I can delete the database file between tests.

    For those interested, this the code in my test tear down:

    1.OaModel ctx = new OaModel();
    2.MethodInfo m = ctx.GetType().GetMethod("GetScope", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
    3.IObjectScope scope = (IObjectScope)m.Invoke(ctx, new object[]{});
    4.ctx.Dispose();
    5.scope.Database.Dispose();
    6.scope.Dispose();

  5. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 08 Oct 2010 Link to this post

    Hi Jakkie,

    Glad you made it work.

    Anyway, a more clean way of getting to the scope is to make a subclass of your OaModel class and expose a public method/property (getter only) that uses the protected method to return the scope.

    Something like (pseudo)

    public partial class OaModelWithScope : OaModel
    {

       public GetObjectScope()
       {
           return this.GetScope();
       }
    }

    The method GetObjectScope will then return the underlying (from the Context perspective) ObjectScope attached to the context.

    PS. Please mark question (and preferable also the post) as answered as it makes it easier for others to find the answer for a similar question.

    regards

    Henrik
  6. Jakkie Esschert van den
    Jakkie Esschert van den avatar
    61 posts
    Member since:
    Sep 2009

    Posted 11 Oct 2010 Link to this post

    I realize I could subclass (or just extend the partial class) to expose the scope. However, I think it is correct to hide the scope in the application code, I'd rather not change my production code just because I need something when testing.

    For testing I don't care about using reflection to achieve something, and I will probably add a bit more so I can reuse this code when testing any OpenAccess based application.

Back to Top
DevCraft banner