• 悲观并发控制


        public abstract class EntityBase
        {
            private int Version { get; set; }
        }
        /// <summary>
        /// 当从数据库中检索出Person实体时设置Version属性。
        /// </summary>
        public class Person : EntityBase
        {
            public Guid Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int Version { get; set; }
        }
    Person
        public interface IPersonRepository
        {
            void Add(Person person);
            void Save(Person person);
            Person FindBy(Guid Id);
        }
    IPersonRepository
        public class PersionRepository : IPersonRepository
        {
            private string _connectionString;
            private string _findByIdSql = "select * from people where PersonId=@PersonId";
            private string _insertSql = "insert people (FristName,LastName,PersonId,Version) values (@FirstName,@LastName,@PersonId,@Version)";
            private string _updateSql = "update people SET FirstName=@FirstName,LastName=@LastName,Version=@Version+1 where PersonId=@PersonId AND Version=@Version";
    
            public PersionRepository(string connectionString)
            {
                _connectionString = connectionString;
            }
    
            public void Add(Person person)
            {
                using (SqlConnection connection = new SqlConnection(_connectionString))
                {
                    SqlCommand command = connection.CreateCommand();
                    command.CommandText = _insertSql;
                    command.Parameters.Add(new SqlParameter("@PersonId", person.Id));
                    command.Parameters.Add(new SqlParameter("@Version", person.Version));
                    command.Parameters.Add(new SqlParameter("@FirstName", person.FirstName));
                    command.Parameters.Add(new SqlParameter("@LastName", person.LastName));
                    connection.Open();
                    command.ExecuteNonQuery();
                }
            }
    
            public void Save(Person person)
            {
                int numberOfRecordsAffected = 0;
                using (SqlConnection connection = new SqlConnection(_connectionString))
                {
                    SqlCommand command = connection.CreateCommand();
                    command.CommandText = _updateSql;
                    command.Parameters.Add(new SqlParameter("@PersonId", person.Id));
                    command.Parameters.Add(new SqlParameter("@Version", person.Version));
                    command.Parameters.Add(new SqlParameter("@FirstName", person.FirstName));
                    command.Parameters.Add(new SqlParameter("@LastName", person.LastName));
                    connection.Open();
                    numberOfRecordsAffected = command.ExecuteNonQuery();
                }
                if (numberOfRecordsAffected == 0)
                    throw new ApplicationException("No changes....");
                else
                {
                    person.Version++;
                }
            }
    
            public Person FindBy(Guid Id)
            {
                Person person = default(Person);
                using (SqlConnection connection = new SqlConnection(_connectionString))
                {
                    SqlCommand command = connection.CreateCommand();
                    command.CommandText = _findByIdSql;
                    command.Parameters.Add(new SqlParameter("@PersonId", Id));
                    connection.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            person = new Person
                            {
                                FirstName = reader["FristName"].ToString(),
                                LastName = reader["LastName"].ToString(),
                                Id = new Guid(reader["PersonId"].ToString()),
                                Version = int.Parse(reader["Version"].ToString())
                            };
                        }
                    }
                }
                return person;
            }
        }
    PersionRepository
  • 相关阅读:
    前端历险记
    Pandas中空值的判断方法,包括数值型、字符串型、时间类型等
    【MySQL】explicit_defaults_for_timestamp 参数详解
    python并发编程--线程---从菜鸟到老鸟(一)
    远程jupyter+pycharm配置 (一)之安装与使用技巧
    airflow--Error: Already running on PID 22603 (or pid file '/home/rdev/airflow/airflow-webserver.pid' is stale)
    数仓调度研究-总论
    pandas实现hive的lag和lead函数 以及 first_value和last_value函数
    iOS开发小技巧--修改按钮内部图片和文字之间的间距(xib)
    iOS开发中的错误整理,关于用绑定Tag取控件的注意事项,有时候不绑定也是个错!
  • 原文地址:https://www.cnblogs.com/vichin/p/13092112.html
Copyright © 2020-2023  润新知