多线程C#应用程序-MongoDB插入期间没有错误地丢失了记录

问题描述 投票:1回答:1

我正在使用Mongo Db C#驱动程序2.0.1.27版和Mongo Db 3.0版。

我们的目标是使用“插入”方法将大量文档插入MongoDb集合。

我们的体系结构为每个线程多次调用此Add方法。下面是Add方法:

public bool Add(CallContext context, FileQueueEntity entity)
        {
            bool bResult = false;
            // This logic is to prevent duplicate file.
            // Consider new algorithm if supporting other files types
            bResult = Delete(context, entity);
            if (context.ErrorList.Count == 0)
            {
                var server = GetMongoServer();
                try
                {
                    var database = GetMongoDatabase(server);
                    var collection = database.GetCollection<FileQueueEntity>("QueueCollection");

                    entity.BaseMeta = null;
                    entity.IsNew = false;
                    collection.Insert(entity);
                    context.AddToUpdatedList(entity);
                }
                catch (Exception ex)
                {
                    bResult = false;
                    context.AddError(ErrorSeverity.System, "DataAccess.AddFileQueue", GetThreadExceptionMessage(ex));
                }
                finally
                {
                }
            }
            return bResult;
        }

下面是获取MongoDatabase方法:

private MongoDatabase GetMongoDatabase(MongoServer mongoServer)
        {
            return mongoServer.GetDatabase(mConnectionBuilder.InitialCatalog);
        }

下面是GetMongoServer的那个

private MongoServer GetMongoServer()
        {
            System.Threading.Monitor.Enter(_lock);
            try
            {
                if (_mongoServer != null)
                {
                    return _mongoServer;
                }

                DatabaseProviderFactory factory = new DatabaseProviderFactory();
                var aDatabase = factory.Create("ConnectionStringName");
                mConnectionBuilder = new SqlConnectionStringBuilder(aDatabase.ConnectionString);
                var credential = MongoCredential.CreateCredential(mConnectionBuilder.InitialCatalog, mConnectionBuilder.UserID, mConnectionBuilder.Password);
                MongoServerSettings databaseSettings = new MongoServerSettings();

                var connectionStrings = mConnectionBuilder.DataSource.Split(',');

                if (connectionStrings != null && connectionStrings.Count() > 1)
                {
                    string ipAddress = connectionStrings[0];
                    int portNumber = Convert.ToInt32(connectionStrings[1], CultureInfo.InvariantCulture);
                    databaseSettings.Credentials = new[] { credential };
                    databaseSettings.Server = new MongoServerAddress(ipAddress, portNumber);
                }

                _mongoServer = new MongoServer(databaseSettings);
                return _mongoServer;
            }
            finally
            {
                System.Threading.Monitor.Exit(_lock);
            }

        }

这就是这样称呼:

foreach (var n in entities)
{
    Add(n);
}

分别为每个实例调用Foreach循环。

问题是,我们看到所有文件都没有到达数据库,因为每次都有表格中缺少的随机文件。

我们正在发送的实体非常轻(几乎为400-500字节)。文件数最多为2000-5000,每天都会清除。因此,在这种情况下,不会超过最大存储空间

例如:

Thread 1: 50 files  -   Random 48 files are inserted
Thread 2: 80 files  -   Random 75 files are inserted
Thread 3: 70 files  -   Random 60 files are inserted
Thread 4: 60 files  -   Random 59 files are inserted

我们是否缺少任何Mongo配置,因为它没有引发任何异常并且无法静默插入记录,有点奇怪。

我们正在插入的响应是

Response: { "ok" : 1, "n" : NumberLong(0) }

观察到每个线程的所有时间随机文件每次都失败。

有人可以帮我吗?我们是否缺少任何MongoDB配置?

我正在使用Mongo Db C#驱动程序2.0.1.27版和Mongo Db 3.0版。我们的目标是使用“插入”方法将大量文档插入MongoDb集合。我们的体系结构称为...

c# mongodb multithreading
1个回答
0
投票

要考虑的几点:

© www.soinside.com 2019 - 2024. All rights reserved.