public async Task Test_Nested_Transactions_Inner_Rollback(Type dataContextType) { // Arrange DataContextTestHelper.CreateTestDatabase(dataContextType); IConnectionProvider connectionProvider = DataContextTestHelper.GetConnectionProvider(dataContextType); // Act / Assert using (IDataContext dataContext1 = DataContextTestHelper.GetDataContext(dataContextType, connectionProvider)) { dataContext1.BeginTransaction(); // creates new transaction await dataContext1.Create(new Product() { Name = "Product 1" }).ConfigureAwait(false); // create second data context with same connection provider using (IDataContext dataContext2 = DataContextTestHelper.GetDataContext(dataContextType, connectionProvider)) { dataContext2.BeginTransaction(); // adds a reference to the outer transaction await dataContext2.Create(new Product() { Name = "Product 2" }).ConfigureAwait(false); await dataContext2.Create(new Product() { Name = "Product 3" }).ConfigureAwait(false); // rollsback the transaction and throws exception try { dataContext2.Rollback(); } catch (Exception ex) { Assert.IsTrue(ex.GetType().IsAssignableFrom(typeof(InvalidOperationException))); } } } // Assert using IDataContext dataContext3 = DataContextTestHelper.GetDataContext(dataContextType, connectionProvider); var products = await dataContext3.ReadAll <Product>(); Assert.AreEqual(0, products.Count()); }
public async Task Test_Nested_Transactions_Outer_Rollback(Type dataContextType) { // Arrange DataContextTestHelper.CreateTestDatabase(dataContextType); IConnectionProvider connectionProvider = DataContextTestHelper.GetConnectionProvider(dataContextType); // Act using (IDataContext dataContext1 = DataContextTestHelper.GetDataContext(dataContextType, connectionProvider)) { dataContext1.BeginTransaction(); // creates new transaction await dataContext1.Create(new Product() { Name = "Product 1" }).ConfigureAwait(false); // create second data context with same connection provider using (IDataContext dataContext2 = DataContextTestHelper.GetDataContext(dataContextType, connectionProvider)) { dataContext2.BeginTransaction(); // adds a reference to the outer transaction await dataContext2.Create(new Product() { Name = "Product 2" }).ConfigureAwait(false); await dataContext2.Create(new Product() { Name = "Product 3" }).ConfigureAwait(false); dataContext2.Commit(); // removes a refernce from the outer transaction but doesn't commit } await dataContext1.Create(new Product() { Name = "Product 3" }).ConfigureAwait(false); dataContext1.Rollback(); } // Assert using IDataContext dataContext3 = DataContextTestHelper.GetDataContext(dataContextType, connectionProvider); var products = await dataContext3.ReadAll <Product>(); Assert.AreEqual(0, products.Count()); }