zoukankan      html  css  js  c++  java
  • 悲观并发控制

        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
  • 相关阅读:
    caffe杂
    easyui 扩展layout的方法,支持动态添加删除块
    easyui换主题,并记录在cookie
    $.messager.show扩展:指定位置显示
    easyui 扩展 之 Tree的simpleData加载
    easyui menu 添加hideItem/showItem 方法
    HTML标签及属性大全
    适应各种内核浏览器的透明修过样式
    让IE6支持min-width和max-width的方法
    javascript获取html标记的的绝对定位值
  • 原文地址:https://www.cnblogs.com/vichin/p/13092112.html
Copyright © 2011-2022 走看看