コード例 #1
0
 static BaseTest()
 {
     for (int i = 1; i <= 100; i++)
     {
         Base_UnitTest newData = new Base_UnitTest
         {
             Id       = Guid.NewGuid().ToString(),
             Age      = i,
             UserId   = "Admin" + i,
             UserName = "******" + i
         };
         _dataList.Add(newData);
     }
 }
コード例 #2
0
 static BaseTest()
 {
     for (int i = 1; i <= 100; i++)
     {
         Base_UnitTest newData = new Base_UnitTest
         {
             Id       = IdHelper.GetId(),
             Age      = i,
             UserId   = "Admin" + i,
             UserName = "******" + i
         };
         _dataList.Add(newData);
     }
 }
コード例 #3
0
        public void DistributedTransaction()
        {
            //失败事务
            var db1 = ServiceProvider.GetService <ISQLiteDb1>();
            var db2 = ServiceProvider.GetService <ISQLiteDb2>();

            db1.DeleteAll <Base_UnitTest>();
            db2.DeleteAll <Base_UnitTest>();
            Base_UnitTest data1 = new Base_UnitTest
            {
                Id       = Guid.NewGuid().ToString(),
                UserId   = "1",
                UserName = Guid.NewGuid().ToString()
            };
            Base_UnitTest data2 = new Base_UnitTest
            {
                Id       = data1.Id,
                UserId   = "1",
                UserName = Guid.NewGuid().ToString()
            };
            Base_UnitTest data3 = new Base_UnitTest
            {
                Id       = Guid.NewGuid().ToString(),
                UserId   = "2",
                UserName = Guid.NewGuid().ToString()
            };

            new Action(() =>
            {
                var transaction = DistributedTransactionFactory.GetDistributedTransaction();
                transaction.AddDbAccessor(db1, db2);
                var succcess = transaction.RunTransaction(() =>
                {
                    db1.ExecuteSql("insert into Base_UnitTest(Id,CreateTime) values('10',@CreateTime) ", ("@CreateTime", DateTime.Now));
                    db1.Insert(data1);
                    db1.Insert(data2);
                    db2.Insert(data1);
                    db2.Insert(data3);
                });
                Assert.IsFalse(succcess.Success);
                Assert.AreEqual(0, db1.GetIQueryable <Base_UnitTest>().Count());
                Assert.AreEqual(0, db2.GetIQueryable <Base_UnitTest>().Count());
            })();

            //成功事务
            new Action(() =>
            {
                var transaction = DistributedTransactionFactory.GetDistributedTransaction();
                transaction.AddDbAccessor(db1, db2);

                var succcess = transaction
                               .RunTransaction(() =>
                {
                    db1.ExecuteSql("insert into Base_UnitTest(Id,CreateTime) values('10',@CreateTime) ", ("@CreateTime", DateTime.Now));
                    db1.Insert(data1);
                    db1.Insert(data3);
                    db2.Insert(data1);
                    db2.Insert(data3);
                });
                int count1 = db1.GetIQueryable <Base_UnitTest>().Count();
                int count2 = db2.GetIQueryable <Base_UnitTest>().Count();
                Assert.IsTrue(succcess.Success);
                Assert.AreEqual(3, count1);
                Assert.AreEqual(2, count2);
            })();
        }
コード例 #4
0
        public void DistributedTransaction()
        {
            //失败事务
            IRepository _db1 = DbFactory.GetRepository("DataSource=db.db", DatabaseType.SQLite);
            IRepository _db2 = DbFactory.GetRepository("DataSource=db2.db", DatabaseType.SQLite);

            _db1.DeleteAll <Base_UnitTest>();
            _db2.DeleteAll <Base_UnitTest>();
            Base_UnitTest data1 = new Base_UnitTest
            {
                Id       = Guid.NewGuid().ToString(),
                UserId   = "1",
                UserName = Guid.NewGuid().ToString()
            };
            Base_UnitTest data2 = new Base_UnitTest
            {
                Id       = data1.Id,
                UserId   = "1",
                UserName = Guid.NewGuid().ToString()
            };
            Base_UnitTest data3 = new Base_UnitTest
            {
                Id       = Guid.NewGuid().ToString(),
                UserId   = "2",
                UserName = Guid.NewGuid().ToString()
            };

            new Action(() =>
            {
                var transaction = DistributedTransactionFactory.GetDistributedTransaction();
                transaction.AddRepository(_db1, _db2);
                var succcess = transaction.RunTransaction(() =>
                {
                    _db1.ExecuteSql("insert into Base_UnitTest(Id) values('10') ");
                    _db1.Insert(data1);
                    _db1.Insert(data2);
                    _db2.Insert(data1);
                    _db2.Insert(data3);
                });
                Assert.IsFalse(succcess.Success);
                Assert.AreEqual(0, _db1.GetIQueryable <Base_UnitTest>().Count());
                Assert.AreEqual(0, _db2.GetIQueryable <Base_UnitTest>().Count());
            })();

            //成功事务
            new Action(() =>
            {
                var transaction = DistributedTransactionFactory.GetDistributedTransaction();
                transaction.AddRepository(_db1, _db2);

                var succcess = transaction
                               .RunTransaction(() =>
                {
                    _db1.ExecuteSql("insert into Base_UnitTest(Id) values('10') ");
                    _db1.Insert(data1);
                    _db1.Insert(data3);
                    _db2.Insert(data1);
                    _db2.Insert(data3);
                });
                int count1 = _db1.GetIQueryable <Base_UnitTest>().Count();
                int count2 = _db2.GetIQueryable <Base_UnitTest>().Count();
                Assert.IsTrue(succcess.Success);
                Assert.AreEqual(3, count1);
                Assert.AreEqual(2, count2);
            })();
        }
コード例 #5
0
        public void DistributedTransaction()
        {
            //失败事务
            IDbAccessor _db1 = DbFactory.GetDbAccessor(Config.SQLITE1, DatabaseType.SQLite);
            IDbAccessor _db2 = DbFactory.GetDbAccessor(Config.SQLITE2, DatabaseType.SQLite);

            _db1.DeleteAll <Base_UnitTest>();
            _db2.DeleteAll <Base_UnitTest>();
            Base_UnitTest data1 = new Base_UnitTest
            {
                Id       = Guid.NewGuid().ToString(),
                UserId   = "1",
                UserName = Guid.NewGuid().ToString()
            };
            Base_UnitTest data2 = new Base_UnitTest
            {
                Id       = data1.Id,
                UserId   = "1",
                UserName = Guid.NewGuid().ToString()
            };
            Base_UnitTest data3 = new Base_UnitTest
            {
                Id       = Guid.NewGuid().ToString(),
                UserId   = "2",
                UserName = Guid.NewGuid().ToString()
            };

            new Action(() =>
            {
                var transaction = DistributedTransactionFactory.GetDistributedTransaction();
                transaction.AddDbAccessor(_db1, _db2);
                var succcess = transaction.RunTransaction(() =>
                {
                    _db1.ExecuteSql("insert into Base_UnitTest(Id,CreateTime) values('10',@CreateTime) ", ("@CreateTime", DateTime.Now));
                    _db1.Insert(data1);
                    _db1.Insert(data2);
                    _db2.Insert(data1);
                    _db2.Insert(data3);
                });
                Assert.IsFalse(succcess.Success);
                Assert.AreEqual(0, _db1.GetIQueryable <Base_UnitTest>().Count());
                Assert.AreEqual(0, _db2.GetIQueryable <Base_UnitTest>().Count());
            })();

            //成功事务
            new Action(() =>
            {
                var transaction = DistributedTransactionFactory.GetDistributedTransaction();
                transaction.AddDbAccessor(_db1, _db2);

                var succcess = transaction
                               .RunTransaction(() =>
                {
                    _db1.ExecuteSql("insert into Base_UnitTest(Id,CreateTime) values('10',@CreateTime) ", ("@CreateTime", DateTime.Now));
                    _db1.Insert(data1);
                    _db1.Insert(data3);
                    _db2.Insert(data1);
                    _db2.Insert(data3);
                });
                int count1 = _db1.GetIQueryable <Base_UnitTest>().Count();
                int count2 = _db2.GetIQueryable <Base_UnitTest>().Count();
                Assert.IsTrue(succcess.Success);
                Assert.AreEqual(3, count1);
                Assert.AreEqual(2, count2);
            })();
        }