Show / Hide Table of Contents

    IAdoDbTransactionManager Interface

    .NET Standard 2.x

    Provides methods to manage database transactions.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

       public interface IAdoDbTransactionManager;
    

    Method

    Name Returns Type Description
    BeginTransaction() IAdoDbTransaction Starts a database transaction with the unspecified isolation level (IsolationLevel.Unspecified).
    BeginTransaction(IsolationLevel isolationLevel) IAdoDbTransaction Starts a database transaction with the specified isolation level.
    BeginTransactionAsync(CancellationToken cancellationToken) Task<IAdoDbTransaction> Asynchronously starts a database transaction without specifying the isolation level (IsolationLevel.Unspecified).
    BeginTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken = default) Task<IAdoDbTransaction> Asynchronously starts a database transaction with the specified isolation level.
    UseTransaction(DbTransaction, transaction) IAdoDbTransaction Uses a specified database transaction when no transaction is already started.

    Examples

    Example 1

    The following code example demonstrates how to use the methods of the IAdoDbTransactionManager object.

    using Appeon.ApiDoc.Models.School;
    using SnapObjects.Data;
    using System;
    using System.Data;
    using System.Threading;
    
    namespace Appeon.ApiDoc.IAdoDbTransactionManagerExamples
    {
        public class IAdoDbTransactionManagerExample
        {
            private readonly SchoolContext _context;
            
            // Declares an IAdoDbTransactionManager object
            private IAdoDbTransactionManager managerTrans;
            
            public IAdoDbTransactionManagerExample(SchoolContext context)
            {
                _context = context;
    
                // Instantiates an IAdoDbTransactionManager object
                managerTrans = _context.CurrentConnection;
            }
    
            public void Example1()
            {
                IAdoDbTransaction transobject;
    
                // Starts a transaction and assigns the current 
                // transaction to the IAdoDbTransactionManager object
                transobject = managerTrans.BeginTransaction();
    
                // Gets the transaction ID and DbTransaction
                var transId = transobject.TransactionId;
                var dbtrans = transobject.DbTransaction;
    
                // Performs data saving operation in the current transaction
                var mapper = _context.SqlModelMapper;
    
                var person = new Person()
                {
                    LastName = "Senior",
                    FirstName = "Letitia",
                    EnrollmentDate = new DateTime(2019, 1, 1),
                    Discriminator = "Student"
                };
    
                try
                {
                    mapper.TrackCreate(person);
                    mapper.SaveChanges();
    
                    // Commits changes to the database
                    transobject.Commit();
    
                    // Shows a success message and the transaction ID 
                    // if data is saved successfully
                    Console.WriteLine("Save Data Success, TransactionId = {0}", transId);
                }
                catch (Exception e)
                {
                    // Rolls back the transaction if failed to save data
                    transobject.Rollback();
    
                    // Shows the error message and the transaction ID
                    Console.WriteLine("{0}, TransactionId = {1}", e.Message, transId);
    
                    // Resets DbTransactionManager
                    managerTrans.UseTransaction(dbtrans);
                }
    
                /* The code produces the following output:
                
                Save Data Success, TransactionId = 56ece99e-0498-4865-8103-9778adae7553
                */
            }
        }
    }
    

    Example 2

    The following code example demonstrates how to use the methods of the IAdoDbTransactionManager object with the specified isolation level.

            public void Example2()
            {
                IAdoDbTransaction transobject;
    
                // Starts a transaction, assigns the current transaction to the
                // IAdoDbTransactionManager object, and specifies the isolation level.
                transobject = managerTrans.BeginTransaction(IsolationLevel.ReadCommitted);
    
                // Gets the transaction ID and DbTransaction
                var transId = transobject.TransactionId;
                var dbtrans = transobject.DbTransaction;
    
                // Performs data saving operation in the current transaction
                var mapper = _context.SqlModelMapper;
    
                var person = new Person()
                {
                    LastName = "Senior",
                    FirstName = "Letitia",
                    EnrollmentDate = new DateTime(2019, 1, 1),
                    Discriminator = "Student"
                };
    
                try
                {
                    mapper.TrackCreate(person);
                    mapper.SaveChanges();
    
                    // Commits changes to the database
                    transobject.Commit();
    
                    // Shows a success message and the transaction ID 
                    // if data is saved successfully
                    Console.WriteLine("Save Data Success, TransactionId = {0}", transId);
                }
                catch (Exception e)
                {
                    // Rolls back the transaction if failed to save data
                    transobject.Rollback();
    
                    // Shows the error message and the transaction ID
                    Console.WriteLine("{0}, TransactionId = {1}", e.Message, transId);
    
                    // Resets DbTransactionManager
                    managerTrans.UseTransaction(dbtrans);
                }
    
                /* The code produces the following output:
                
                Save Data Success, TransactionId = 56ece99e-0498-4865-8103-9778adae7553
                */
            }
    

    Example 3

    The following code example demonstrates how to use the methods of the IAdoDbTransactionManager object asynchronously. It uses a CancellationTokenSource type parameter to make this operation cancelable.

            public async void Example3(CancellationTokenSource cts)
            {
                IAdoDbTransaction transobject;
    
                try
                {
                    // Starts a transaction asynchronously and assigns the current transaction 
                    // to the IAdoDbTransactionManager object. If a task has been cancelled, 
                    // the call to the IAdoDbTransactionManager.BeginTransactionAsync method throws 
                    // an OperationCanceledException.
                    transobject = await managerTrans.BeginTransactionAsync(cts.Token);
    
                    // Gets the transaction ID and DbTransaction
                    var transId = transobject.TransactionId;
                    var dbtrans = transobject.DbTransaction;
    
                    // Performs data saving operation in the current transaction
                    var mapper = _context.SqlModelMapper;
    
                    var person = new Person()
                    {
                        LastName = "Senior",
                        FirstName = "Letitia",
                        EnrollmentDate = new DateTime(2019, 1, 1),
                        Discriminator = "Student"
                    };
    
                    try
                    {
                        mapper.TrackCreate(person);
                        await mapper.SaveChangesAsync();
    
                        // Commits changes to the database
                        await transobject.CommitAsync();
    
                        // Shows a success message and the transaction ID 
                        // if data is saved successfully
                        Console.WriteLine("Save Data Success, TransactionId = {0}", transId);
                    }
                    catch (Exception e)
                    {
                        // Rolls back the transaction if failed to save data
                        await transobject.RollbackAsync();
    
                        // Shows the error message and the transaction ID
                        Console.WriteLine("{0}, TransactionId = {1}", e.Message, transId);
    
                        // Resets DbTransactionManager
                        managerTrans.UseTransaction(dbtrans);
                    }
                }
                catch (OperationCanceledException e)
                {
                    // The operation was cancelled before completion.
                    Console.WriteLine(e.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    cts.Dispose();
                }
    
                /* The code produces the following output:
                
                    A task was canceled.
                */
            }
    

    Example 4

    The following code example demonstrates how to use the methods of the IAdoDbTransactionManager object with the specified isolation level. It uses a CancellationTokenSource type parameter to make this operation cancelable.

            public async void Example4(CancellationTokenSource cts)
            {
                IAdoDbTransaction transobject;
    
                try
                {
                    // Starts a transaction asynchronously, assigns the current transaction to the 
                    // IAdoDbTransactionManager object, and specifies the isolation level. If a task 
                    // has been cancelled, the call to the IAdoDbTransactionManager.BeginTransactionAsync
                    // method throws an OperationCanceledException.
                    transobject = await managerTrans.BeginTransactionAsync(IsolationLevel.ReadCommitted, cts.Token);
    
                    // Gets the transaction ID and DbTransaction
                    var transId = transobject.TransactionId;
                    var dbtrans = transobject.DbTransaction;
    
                    // Performs data saving operation in the current transaction
                    var mapper = _context.SqlModelMapper;
    
                    var person = new Person()
                    {
                        LastName = "Senior",
                        FirstName = "Letitia",
                        EnrollmentDate = new DateTime(2019, 1, 1),
                        Discriminator = "Student"
                    };
    
                    try
                    {
                        mapper.TrackCreate(person);
                        mapper.SaveChanges();
    
                        // Commits changes to the database
                        transobject.Commit();
    
                        // Shows a success message and the transaction ID 
                        // if data is saved successfully
                        Console.WriteLine("Save Data Success, TransactionId = {0}", transId);
                    }
                    catch (Exception e)
                    {
                        // Rolls back the transaction if failed to save data
                        transobject.Rollback();
    
                        // Shows the error message and the transaction ID
                        Console.WriteLine("{0}, TransactionId = {1}", e.Message, transId);
    
                        // Resets DbTransactionManager
                        managerTrans.UseTransaction(dbtrans);
                    }
                }
                catch (OperationCanceledException e)
                {
                    // The operation was cancelled before completion.
                    Console.WriteLine(e.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    cts.Dispose();
                }
    
                /* The code produces the following output:
                
                    A task was canceled.
                */
            }
    

    Example(s) Refer To

    Model Class: Person

    Back to top Generated by Appeon