コード例 #1
0
        public async Task BeginTransactionAsync(CancellationToken cancellationToken = default)
        {
            var stopwatch = Logger.StartStopwatch();

            Logger.LogInformation($"Start BeginTransactionAsync");

            if (!IsConnected)
            {
                await CheckStateAndConnectAsync(cancellationToken);

                return;
            }

            if (await IsInTransactionAsync(cancellationToken))
            {
                Logger.LogWarning("Transaction already began");
                return;
            }

            var session = await CreateSessionAsync(Client, cancellationToken);

            if (_currentTransaction == null)
            {
                _currentTransaction = new MongoDbTransaction(session);
            }
            else
            {
                _currentTransaction.SessionHandle = session;
            }

            stopwatch?.Stop();
            Logger.LogInformation("End BeginTransactionAsync, elapsed time: {elapsedTime}", Logger.GetElapsedTime(stopwatch));
            stopwatch = null;
        }
コード例 #2
0
        CheckStateAndConnectInternalAsync(CancellationToken cancellationToken = default)
        {
            var stopwatch = Logger.StartStopwatch();

            Logger.LogInformation($"Start CheckStateAndConnectInternalAsync");

            var client   = new MongoClient(_dbContextOptions.Settings);
            var database = client.GetDatabase(DatabaseName);
            IClientSessionHandle session = null;

            var isTransactional = UnitOfWork?.IsTransactional ?? false;

            if (isTransactional)
            {
                session = await CreateSessionAsync(client, cancellationToken);

                if (_currentTransaction == null)
                {
                    _currentTransaction = new MongoDbTransaction(session);
                }
                else
                {
                    _currentTransaction.SessionHandle = session;
                }
            }

            stopwatch?.Stop();
            Logger.LogInformation("End CheckStateAndConnectInternalAsync, elapsed time: {elapsedTime}", Logger.GetElapsedTime(stopwatch));
            stopwatch = null;

            return(client, database, session);
        }
コード例 #3
0
        public void BeginTransaction()
        {
            var stopwatch = Logger.StartStopwatch();

            Logger.LogInformation($"Start BeginTransaction");

            if (!IsConnected)
            {
                CheckStateAndConnect();
                return;
            }

            if (IsInTransaction())
            {
                Logger.LogWarning("Transaction already began");
                return;
            }

            var session = CreateSession(Client);

            if (_currentTransaction == null)
            {
                _currentTransaction = new MongoDbTransaction(session);
            }
            else
            {
                _currentTransaction.SessionHandle = session;
            }

            stopwatch?.Stop();
            Logger.LogInformation("End BeginTransaction, elapsed time: {elapsedTime}", Logger.GetElapsedTime(stopwatch));
            stopwatch = null;
        }