Esempio n. 1
0
        /// <summary>
        /// Use optimistic concurrency to update the  semaphore owners
        /// </summary>
        /// <param name="origRowVersion"></param>
        /// <param name="owners"></param>
        /// <returns></returns>
        private async Task <bool> _updateSemaphoreInfo(byte[] origRowVersion, SemaphoreOwners owners)
        {
            using (var conn = _dbConnection.Get())
                using (DbCommand command = conn.CreateCommand())
                {
                    var sql = @"
                    UPDATE __Semaphores SET [Owners] = @Owners 
                    WHERE [Name] = @SemaphoreName AND 
                    [RowVersion] = @RowVersion                   
                    ";

                    conn.Open();
                    command.CommandText = sql;
                    SqlParameter nameParam = new SqlParameter("@SemaphoreName", this.Name)
                    {
                        SqlDbType = SqlDbType.NVarChar,
                        Direction = ParameterDirection.Input,
                    };
                    SqlParameter ownersParam = new SqlParameter("@Owners", Serialize(owners))
                    {
                        SqlDbType = SqlDbType.NVarChar,
                        Direction = ParameterDirection.Input,
                    };
                    command.Parameters.Add(ownersParam);
                    command.Parameters.Add(nameParam);
                    SqlParameter versionParam = new SqlParameter("@RowVersion", origRowVersion)
                    {
                        SqlDbType = SqlDbType.Timestamp,
                        Direction = ParameterDirection.Input,
                    };
                    command.Parameters.Add(versionParam);

                    int rows = await command.ExecuteNonQueryAsync();

                    if (rows == 1)
                    {
                        return(true);
                    }

                    return(false);
                }
        }
Esempio n. 2
0
 public string Serialize(SemaphoreOwners owners)
 {
     return(JsonConvert.SerializeObject(owners));
 }