Example #1
0
        static void LockTestFn()
        {
            SQLiteContext context = new SQLiteContext(new SQLiteConnectionFactory(ConnString));

            try
            {
                for (int i = 0; i < 2; i++)
                {
                    context.BeginTransaction(IsolationLevel.Unspecified);
                    try
                    {
                        var users = context.Query <User>().ToList();
                        context.Update <User>(a => a.Id == 12, a => new User()
                        {
                            Age = a.Age + 1
                        });
                        context.Query <User>().ToList();
                        context.Query <User>().ToList();
                        context.Update <User>(a => a.Id == 12, a => new User()
                        {
                            Age = a.Age + 1
                        });
                        context.Update <User>(a => a.Id == 12, a => new User()
                        {
                            Age = a.Age + 1
                        });

                        //if (i == 1)
                        //    throw new Exception("error");

                        context.CommitTransaction();
                    }
                    catch
                    {
                        context.RollbackTransaction();
                        throw;
                    }
                }

                Console.WriteLine(string.Format("执行完毕:{0}", Thread.CurrentThread.ManagedThreadId.ToString()));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine(ex.Message);
                Console.WriteLine(string.Format("执行出错:{0}", Thread.CurrentThread.ManagedThreadId.ToString()));
                return;
            }

            int ret = System.Threading.Interlocked.Increment(ref finishThreadCount);

            Console.WriteLine(string.Format("finishThreadCount:{0}", ret));
        }
Example #2
0
        public static void DoWithTransaction()
        {
            using (ITransientTransaction tran = context.BeginTransaction())
            {
                /* do some things here */
                context.Update <User>(a => a.Id == 1, a => new User()
                {
                    Name = a.Name, Age = a.Age + 1, Gender = Gender.Man, OpTime = DateTime.Now
                });
                context.Delete <User>(a => a.Id == 1024);

                tran.Commit();
            }

            ConsoleHelper.WriteLineAndReadKey();
        }