Exemplo n.º 1
0
        public async Task DataConnectionCommitTransactionAsync([DataSources(false)] string context)
        {
            using (var db = new DataConnection(context))
                using (await db.BeginTransactionAsync())
                {
                    int tid;
                    try
                    {
                        await db.InsertAsync(new Parent { ParentID = 1010, Value1 = 1010 });

                        tid = Thread.CurrentThread.ManagedThreadId;

                        await db.CommitTransactionAsync();
                    }
                    finally
                    {
                        // perform synchonously to not mess with CommitTransactionAsync testing
                        db.GetTable <Parent>().Where(_ => _.ParentID == 1010).Delete();
                    }

                    if (tid == Thread.CurrentThread.ManagedThreadId)
                    {
                        Assert.Inconclusive("Executed synchronously due to lack of async support or there were no underlying async operations");
                    }
                }
        }
Exemplo n.º 2
0
        public async Task DataConnectionBeginTransactionAsync([DataSources(false)] string context)
        {
            var tid = Thread.CurrentThread.ManagedThreadId;

            using (var db = new DataConnection(context))
                using (await db.BeginTransactionAsync())
                {
                    Assert.AreNotEqual(tid, Thread.CurrentThread.ManagedThreadId);

                    db.Insert(new Parent {
                        ParentID = 1010, Value1 = 1010
                    });
                }
        }
Exemplo n.º 3
0
        public async Task DataConnectionBeginTransactionAsync([DataSources(false)] string context)
        {
            var tid = Thread.CurrentThread.ManagedThreadId;

            using (var db = new DataConnection(context))
                using (await db.BeginTransactionAsync())
                {
                    // perform synchonously to not mess with BeginTransactionAsync testing
                    db.Insert(new Parent {
                        ParentID = 1010, Value1 = 1010
                    });

                    if (tid == Thread.CurrentThread.ManagedThreadId)
                    {
                        Assert.Inconclusive("Executed synchronously due to lack of async support or there were no underlying async operations");
                    }
                }
        }
Exemplo n.º 4
0
        public async Task TraceInfoStepsAreReportedForBeginTransactionIlosationLevelAsync([NorthwindDataContext(
#if NETFRAMEWORK
                                                                                               excludeSqlite: false, excludeSqliteMs: true
#endif
                                                                                               )] string context)
        {
            var events   = GetEnumValues((TraceInfoStep s) => default(TraceInfo));
            var counters = GetEnumValues((TraceInfoStep s) => 0);

            using (var db = new DataConnection(context))
            {
                db.OnTraceConnection = e =>
                {
                    events[e.TraceInfoStep] = e;
                    counters[e.TraceInfoStep]++;
                };
                using (await db.BeginTransactionAsync(IsolationLevel.ReadCommitted))
                {
                    // Begin transaction command is reported on each step
                    Assert.AreEqual("BeginTransactionAsync(ReadCommitted)", events[TraceInfoStep.BeforeExecute] !.CommandText);
                    Assert.AreEqual("BeginTransactionAsync(ReadCommitted)", events[TraceInfoStep.AfterExecute] !.CommandText);

                    db.SetCommand(@"UPDATE Categories SET CategoryName = CategoryName WHERE 1=2").Execute();
                    await db.CommitTransactionAsync();

                    // Commit transaction command is reported on each step
                    Assert.AreEqual("CommitTransactionAsync", events[TraceInfoStep.BeforeExecute] !.CommandText);
                    Assert.AreEqual("CommitTransactionAsync", events[TraceInfoStep.AfterExecute] !.CommandText);

                    // steps called once for BeginTransaction once for Update and once for CommitTransaction
                    Assert.AreEqual(3, counters[TraceInfoStep.BeforeExecute]);
                    Assert.AreEqual(3, counters[TraceInfoStep.AfterExecute]);

                    // step called once for Update
                    Assert.AreEqual(1, counters[TraceInfoStep.Completed]);

                    // steps never called
                    Assert.AreEqual(0, counters[TraceInfoStep.MapperCreated]);
                    Assert.AreEqual(0, counters[TraceInfoStep.Error]);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Performs autocommit transaction
        /// </summary>
        public static async Task PerformAutoTransactionAsync(this DataConnection dataConnection, Func <DataConnection, Task> func)
        {
            if (dataConnection == null)
            {
                throw new ArgumentNullException(nameof(dataConnection));
            }

            using var transaction = await dataConnection.BeginTransactionAsync();

            try
            {
                await func(dataConnection);

                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Begins a new transaction asynchronously. You must dispose the returned transaction by yourself.
        /// The session will not track any of the transactions that are created via this method.
        /// Furthermore, you should ensure that a previous transaction has been committed before
        /// calling this method again - Linq2Db will dispose the active transaction and create a
        /// new one internally.
        /// </summary>
        public async Task <IAsyncTransaction> BeginTransactionAsync(CancellationToken cancellationToken = default)
        {
            var dataConnectionTransaction = await DataConnection.BeginTransactionAsync(cancellationToken);

            return(new Linq2DbTransaction(dataConnectionTransaction));
        }