This is a migrated thread and some comments may be shown as answers.

Forcing OpenAccess to close the database.

4 Answers 138 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jakkie Esschert van den
Top achievements
Rank 1
Jakkie Esschert van den asked on 08 Oct 2010, 09:49 AM

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?

4 Answers, 1 is accepted

Sort by
0
Accepted
IT-Als
Top achievements
Rank 1
answered on 08 Oct 2010, 12:14 PM
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
0
Jakkie Esschert van den
Top achievements
Rank 1
answered on 08 Oct 2010, 02:14 PM

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();

0
IT-Als
Top achievements
Rank 1
answered on 08 Oct 2010, 02:26 PM
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
0
Jakkie Esschert van den
Top achievements
Rank 1
answered on 11 Oct 2010, 02:26 PM

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.

Tags
Getting Started
Asked by
Jakkie Esschert van den
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
Jakkie Esschert van den
Top achievements
Rank 1
Share this question
or