예제 #1
0
        public override void SetRangeInHash(string key, IEnumerable <KeyValuePair <string, string> > keyValuePairs)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (keyValuePairs == null)
            {
                throw new ArgumentNullException(nameof(keyValuePairs));
            }

            string sql =
                $@";merge [{_storage.SchemaName}].Hash with (holdlock) as Target
using (VALUES (@key, @field, @value)) as Source ([Key], Field, Value)
on Target.[Key] = Source.[Key] and Target.Field = Source.Field
when matched then update set Value = Source.Value
when not matched then insert ([Key], Field, Value) values (Source.[Key], Source.Field, Source.Value);";

            _storage.UseTransaction((connection, transaction) =>
            {
                foreach (var keyValuePair in keyValuePairs)
                {
                    connection.Execute(
                        sql,
                        new { key = key, field = keyValuePair.Key, value = keyValuePair.Value },
                        transaction,
                        commandTimeout: _storage.CommandTimeout);
                }
            });
        }
예제 #2
0
        public override void Commit()
        {
            _storage.UseTransaction((connection, transaction) =>
            {
                if (_lockedResources.Count > 0)
                {
                    connection.Execute(
                        "set nocount on;" +
                        "exec sp_getapplock @Resource=@resource, @LockMode=N'Exclusive'",
                        _lockedResources.Select(x => new { resource = x }),
                        transaction,
                        _storage.CommandTimeout);
                }

                foreach (var command in _commandQueue)
                {
                    command(connection, transaction);
                }
            });

            foreach (var command in _afterCommitCommandQueue)
            {
                command();
            }
        }
        public override void Commit()
        {
            _storage.UseTransaction(_dedicatedConnectionFunc(), (connection, transaction) =>
            {
                using (var commandBatch = new SqlCommandBatch(preferBatching: _storage.CommandBatchMaxTimeout.HasValue))
                {
                    commandBatch.Append("set xact_abort on;set nocount on;");

                    if (!_storage.Options.DisableGlobalLocks)
                    {
                        foreach (var lockedResource in _lockedResources)
                        {
                            commandBatch.Append(
                                "exec sp_getapplock @Resource=@resource, @LockMode=N'Exclusive'",
                                new SqlParameter("@resource", lockedResource));
                        }
                    }

                    AppendBatch(_jobCommands, commandBatch);
                    AppendBatch(_counterCommands, commandBatch);
                    AppendBatch(_hashCommands, commandBatch);
                    AppendBatch(_listCommands, commandBatch);
                    AppendBatch(_setCommands, commandBatch);
                    AppendBatch(_queueCommands, commandBatch);

                    commandBatch.Connection             = connection;
                    commandBatch.Transaction            = transaction;
                    commandBatch.CommandTimeout         = _storage.CommandTimeout;
                    commandBatch.CommandBatchMaxTimeout = _storage.CommandBatchMaxTimeout;

                    commandBatch.ExecuteNonQuery();

                    foreach (var queueCommand in _queueCommandQueue)
                    {
                        queueCommand(connection, transaction);
                    }
                }
            });

            foreach (var command in _afterCommitCommandQueue)
            {
                command();
            }
        }
예제 #4
0
        public override void Commit()
        {
            _storage.UseTransaction(connection =>
            {
                connection.EnlistTransaction(Transaction.Current);

                if (_lockedResources.Count > 0)
                {
                    connection.Execute(
                        "set nocount on;" +
                        "exec sp_getapplock @Resource=@resource, @LockMode=N'Exclusive'",
                        _lockedResources.Select(x => new { resource = x }));
                }

                foreach (var command in _commandQueue)
                {
                    command(connection);
                }
            });
        }
예제 #5
0
        public override void SetRangeInHash(string key, IEnumerable <KeyValuePair <string, string> > keyValuePairs)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (keyValuePairs == null)
            {
                throw new ArgumentNullException(nameof(keyValuePairs));
            }

            string sql =
                $@";merge [{_storage.SchemaName}].Hash with (holdlock) as Target
using (VALUES (@key, @field, @value)) as Source ([Key], Field, Value)
on Target.[Key] = Source.[Key] and Target.Field = Source.Field
when matched then update set Value = Source.Value
when not matched then insert ([Key], Field, Value) values (Source.[Key], Source.Field, Source.Value);";

            _storage.UseTransaction(_dedicatedConnection, (connection, transaction) =>
            {
                var commandBatch = new SqlCommandBatch(preferBatching: _storage.CommandBatchMaxTimeout.HasValue);

                foreach (var keyValuePair in keyValuePairs)
                {
                    commandBatch.Append(sql,
                                        new SqlParameter("@key", key),
                                        new SqlParameter("@field", keyValuePair.Key),
                                        new SqlParameter("@value", (object)keyValuePair.Value ?? DBNull.Value));
                }

                commandBatch.Connection             = connection;
                commandBatch.Transaction            = transaction;
                commandBatch.CommandTimeout         = _storage.CommandTimeout;
                commandBatch.CommandBatchMaxTimeout = _storage.CommandBatchMaxTimeout;

                commandBatch.ExecuteNonQuery();
            });
        }
        public override void Commit()
        {
            _storage.UseTransaction(_dedicatedConnectionFunc(), (connection, transaction) =>
            {
                var commandBatch = new SqlCommandBatch(preferBatching: _storage.CommandBatchMaxTimeout.HasValue);

                foreach (var lockedResource in _lockedResources)
                {
                    commandBatch.Append(
                        "set nocount on;exec sp_getapplock @Resource=@resource, @LockMode=N'Exclusive'",
                        new SqlParameter("@resource", lockedResource));
                }

                foreach (var command in _commandQueue)
                {
                    commandBatch.Append(command.Item1, command.Item2);
                }

                commandBatch.Connection             = connection;
                commandBatch.Transaction            = transaction;
                commandBatch.CommandTimeout         = _storage.CommandTimeout;
                commandBatch.CommandBatchMaxTimeout = _storage.CommandBatchMaxTimeout;

                commandBatch.ExecuteNonQuery();

                foreach (var queueCommand in _queueCommandQueue)
                {
                    queueCommand(connection, transaction);
                }
            });

            foreach (var command in _afterCommitCommandQueue)
            {
                command();
            }
        }
예제 #7
0
 private T UseConnection <T>(Func <SqlConnection, T> action)
 {
     return(_storage.UseTransaction(action, IsolationLevel.ReadUncommitted));
 }