简体   繁体   English

DBContext多线程

[英]DBContext multi-threading

I have a website I've built in VS2010 which is using MVC3 (Razor) 我有一个我在VS2010中构建的网站,它使用的是MVC3(Razor)

I use DBContexts to access the datbase, my connection string is: 我使用DBContexts访问数据库,我的连接字符串是:

<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. 我想要一个游戏循环,所以我在Application_Start中启动了一个新线程,它使用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. 似乎正在发生的是循环使用DBContext并锁定db文件,然后每当您尝试从网站本身的DBContext进行更改时,该文件都在使用中。

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. 不是在我声明DBContext的时候,每当我尝试使用DBContext执行某种搜索或更新时就会发生这种情况。

Here's my Global.asax 这是我的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. 我一直在考虑Singletons,Locks或Synclocks,调整连接字符串以允许多个附加到数据库文件,或让我的循环请求一个执行更新的网页。

Any ideas? 有任何想法吗?

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. 似乎正在发生的是循环使用DBContext并锁定db文件,然后每当您尝试从网站本身的DBContext进行更改时,该文件都在使用中。

Back to reading the documentation? 回去阅读文档? The part where it says WinCE is NOT A SERVER FOR MULTIPLE THREADS. 它说WinCE的部分不是多线程的服务器。 Use SQL Server instead - Express is free . 请改用SQL Server - Express是免费的 CE is for limited scalability items, single threaded access. CE用于有限的可伸缩性项目,单线程访问。

What is the best practice for doing this? 这样做的最佳做法是什么?

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. CE是非常有问题的,特别是如果你一直启动/停止数据库引擎,这会导致很多低效的磁盘访问。

SQL Server is done for exactly that and as I said - Express is cheap. SQL Server正是为此完成的,正如我所说的 - Express很便宜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM