当更多用户可以写入 .dbf 数据库文件时使用锁?

发布于 2024-09-18 20:39:26 字数 1006 浏览 7 评论 0原文

遗憾的是,如果需要的话,我必须在服务器端处理 .dbf 文件或数据库,我有一个问题。 由于 .dbf 位于服务器端,更多用户可以访问它(读取和写入,我使用 C# 和 OdbcConnection)。每当我进行插入/更新时我应该使用锁吗?


我会回答我自己的问题,因为我想粘贴一段代码。 我有一个用于简单操作的基类

void ExecuteNonQuery(string sqlStatement)
T ExecuteScalar<T>(string sqlStatement)
List<T> GetDataTable<T>(string sqlStatement) where T:new()

public class BaseService
{
        protected void ExecuteNonQuery(string sqlStatement)
        {
            using (OdbcConnection odbconn = new OdbcConnection(ConnectionString))
            {
                odbconn.Open();
                OdbcCommand cmd = new OdbcCommand(sqlStatement, odbconn);
                cmd.ExecuteNonQuery();
            }
        }
}

public class UsersService : BaseService
{
        public void SomeInsert()
        {
            string insertUserString = "Insert Into....";
            ExecuteNonQuery(insertUserString);
            return true;
        }
}

,我不知道它是否是最好的解决方案,但这些操作就是我所需要的。我有点困惑如何在这里使用锁。

Sadly, i have to deal with a .dbf file or database if you want, in the server side and i have one question.
Since the .dbf is on the server side more users can access it(read and write, i use C# and OdbcConnection). Should i use lock whenever i make insert/update?


I will answer to my own question because i want to paste a piece of code.
I have a base class for simple operations

void ExecuteNonQuery(string sqlStatement)
T ExecuteScalar<T>(string sqlStatement)
List<T> GetDataTable<T>(string sqlStatement) where T:new()

public class BaseService
{
        protected void ExecuteNonQuery(string sqlStatement)
        {
            using (OdbcConnection odbconn = new OdbcConnection(ConnectionString))
            {
                odbconn.Open();
                OdbcCommand cmd = new OdbcCommand(sqlStatement, odbconn);
                cmd.ExecuteNonQuery();
            }
        }
}

public class UsersService : BaseService
{
        public void SomeInsert()
        {
            string insertUserString = "Insert Into....";
            ExecuteNonQuery(insertUserString);
            return true;
        }
}

I don;t know if it the best solution but those operations were all i need. I am kinda confused how to use lock here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一曲爱恨情仇 2024-09-25 20:39:39

我有 VFP 背景,因此熟悉 DBF 文件的使用。实际上,在处理基本类型连接(例如通过 ODBC)时,如果您一次更新多个表(例如在服务器数据库中使用事务),或者如果您想要更新多个表,则只需对数据应用锁。确保您的插入/更新不会因为另一个用户应用了锁定而失败(因此您的锁定尝试失败而不是数据更改尝试失败)。

I come from a VFP background, and so am familiar with using DBF files. In reality, when dealing with basic type connections like via ODBC, you only need to apply locks to the data if you are updating more than one table at a time (like using a transaction in a server database), or if you want to be sure that your insert / update won't fail because another user has applied a lock (so your lock attempt fails rather than your data change attempt).

冷默言语 2024-09-25 20:39:36

对我来说,这里有一些含糊之处。当你说锁时,你指的是数据库锁还是线程锁?

如果您正在参考

  1. 某种锁定磁盘上文件的数据库锁 -
  2. 线程锁定,这取决于并发访问
    -来自同一进程的线程,然后是监视器可以做到这一点。
    -如果并发来自同一机器上的多个进程,则 互斥体 可以工作。
    -如果并发来自多个单独的服务器,那么无线程锁定解决方案将不起作用。

但你是对的,对于基于文件的数据库没有控制并发访问的引擎,你将需要仔细处理并发访问。

更新 1:

对于简单的单服务器单应用程序**无**网络花园场景,您可以可以编写数据访问层以使用lock(监视器的语法糖)限制对 DBF 文件的并发访问。但如今的小型 Web 应用程序正在演变成需要纵向扩展和横向扩展的应用程序,那么您的锁定解决方案也需要随之发展。

您还应该意识到,您选择的任何解决方案都会影响您的应用程序在额外负载下的扩展程度。即使最好的无服务器数据库(例如 Sqlite)在需要处理来自多个线程的并发访问时也会遭受严重的性能损失。

For me, there is some ambiguity here. When you say lock are you refering to a DB lock or a Thread Locking lock?

If you are reffering to

  1. Some kind of DB Lock which locks the file on disk - Yes
  2. Thread Locking that depends, if the concurrent access from
    -Threads from the same process then yes a Monitor could do the trick.
    -If the concurrency is from multiple processes on the same box then a Mutex could work.
    -If the concurrency is from multiple separate servers then NO thread locking solutions will not work.

But you are correct, with a file based database with no engine to control concurrent access you will need to deal with concurrent access carefully.

Update-1:

For A simple single server, single application, **no** web garden scenario you could write your data access layer to restrict concurrent access to the DBF files using a lock (syntax sugar for a Monitor). But todays small web application evloves into something that needs to scale up and scale out then your locking solution needs to evolve with it.

You should also be aware that what any solution you go for, it is servely impact how well your application scales with additional load. Event the best serverless databases like Sqlite suffer a significant performance hit when they need to deal with concurrent access from multple threads.

贱人配狗天长地久 2024-09-25 20:39:33

当超过 1 个线程想要使用相同的 OdbcConnection 时,您应该锁定您自己的代码。但最好避免这种情况。创建并使用连接,然后尽快处理它们。

您无法真正锁定其他用户(会话)的文件。由 Odbc 提供者来同步该部分。

You should lock in your own code when more than 1 thread wants to use the same OdbcConnection. But it is better to avoid that. Create and Use the connections and then Dispose them as quickly as possible.

You can't really lock the files for other users (sessions). It's up to the Odbc provider to synchronize that part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文