I have a website I’ve built in VS2010 which is using MVC3 (Razor)
I use DBContexts to access the datbase, my connection string is:
<add name="SystemDBContext"
connectionString="Data Source=|DataDirectory|System.sdf"
providerName="System.Data.SqlServerCe.4.0" />
I want to have a game loop so I’ve started a new thread in Application_Start which loops and accesses the database using the DBContext.
What seems to be happening is that the loop uses the DBContext and locks the db file, and then whenever you try to make changes from the DBContext on the website itself, the file is in use.
It’s not when I declare DBContext, it happens whenever I try to use the DBContext to perform a search or update of some sort.
Here’s my Global.asax
protected void Application_Start()
{
...
Manager m = new Manager();
Thread t = new Thread(m.Start);
t.Start();
}
Part of one of my controllers:
public class myController : Controller
{
private SystemDBContext db = new SystemDBContext();
public ViewResult Index()
{
var engines = db.Engines.Include(e => e.state);
return View(engines.ToList());
}
...
And my loop looks something like this:
public class Manager
{
public void Start()
{
while (true)
{
SystemDBContext db = new SystemDBContext();
var query = from ...
}
...
What is the best practice for doing this? I’ve been thinking of Singletons, Locks or Synclocks, adjusting the connection string to allow multiple attaches to the database file, or having my loop request a web page that does the update.
Any ideas?
Back to reading the documentation? The part where it says WinCE is NOT A SERVER FOR MULTIPLE THREADS. Use SQL Server instead – Express is free. CE is for limited scalability items, single threaded access.
Use a proper database server. CE is HEAVILY problematic, especially if you start / stop the database engine all the time, which results into a LOT of inefficient disc access.
SQL Server is done for exactly that and as I said – Express is cheap.