public void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { var context = EFProfilerContextProvider.GetLoggedDbTransaction(transaction, interceptionContext); _profiler.TransactionCommitted(transaction, context); }
void IDbTransactionInterceptor.Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (this.IncludeTargetContext(interceptionContext.DbContexts)) { FlushEntries(); } }
public void Disposing(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { var context = EFProfilerContextProvider.GetLoggedDbTransaction(transaction, interceptionContext); _profiler.TransactionDisposing(transaction, context); }
public void RolledBack(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { var context = EFProfilerContextProvider.GetLoggedDbTransaction(transaction, interceptionContext); _profiler.TransactionRolledBack(transaction, context); }
/// <summary> /// This method is called before <see cref="DbTransaction.Dispose()" /> is invoked. /// The default implementation of this method filters by <see cref="DbContext" /> set into /// <see cref="Context" />, if any, and then logs the event. /// </summary> /// <param name="transaction">The transaction being disposed.</param> /// <param name="interceptionContext">Contextual information associated with the call.</param> public virtual void Disposing(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (ShouldLog(interceptionContext) && transaction.Connection != null) { LogInformation("Disposed transaction at {now}", DateTimeOffset.Now); } }
public void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (interceptionContext.Exception != null) { _timesToFail--; } }
/// <summary> /// This method is called before <see cref="DbTransaction.Dispose()" /> is invoked. /// The default implementation of this method filters by <see cref="DbContext" /> set into /// <see cref="Context" />, if any, and then logs the event. /// </summary> /// <param name="transaction">The transaction being disposed.</param> /// <param name="interceptionContext">Contextual information associated with the call.</param> public virtual void Disposing(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (ShouldLog(interceptionContext) && transaction.Connection != null) { LogInformation("Disposed transaction {0}", transaction.GetHashCode()); } }
void IDbTransactionInterceptor.RolledBack(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (this.IncludeTargetContext(interceptionContext.DbContexts)) { ClearEntries(); } }
public override void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (interceptionContext.Exception != null && (interceptionContext.Connection != null && MatchesParentContext(interceptionContext.Connection, interceptionContext))) { interceptionContext.Exception = new CommitFailedException(Strings.CommitFailed, interceptionContext.Exception); } }
public void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { var entitySets = RemoveAffectedEntitySets(transaction); if (entitySets != null) { ResolveCache(transaction.Connection).InvalidateSets(entitySets.Distinct()); } }
/** <inheritDoc /> */ public void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { HashSet <EntitySetBase> entitySets; if (_entitySets.TryGetValue(transaction, out entitySets)) { _cache.InvalidateSets(entitySets); } }
public void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { var entitySets = RemoveAffectedEntitySets(transaction); if (entitySets != null) { _cache.InvalidateSets(entitySets.Distinct(), new DbInfo(interceptionContext.Connection.Database)); } }
public void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { var entitySets = RemoveAffectedEntitySets(transaction); if (entitySets != null) { _cache.InvalidateSets(entitySets); _cache.RequestInvalidateSets(entitySets); } }
public override void Committed( DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (interceptionContext.Exception == null || interceptionContext.Connection == null || !this.MatchesParentContext(interceptionContext.Connection, (DbInterceptionContext)interceptionContext)) { return; } interceptionContext.Exception = (Exception) new CommitFailedException(Strings.CommitFailed, interceptionContext.Exception); }
/// <summary> /// If there was an exception thrown checks the database for this transaction and rethrows it if not found. /// Otherwise marks the commit as succeeded and queues the transaction information to be deleted. /// </summary> /// <param name="transaction">The transaction that was commited.</param> /// <param name="interceptionContext">Contextual information associated with the call.</param> /// <seealso cref="IDbTransactionInterceptor.Committed" /> public override void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { TransactionRow transactionRow; if (TransactionContext == null || (interceptionContext.Connection != null && !MatchesParentContext(interceptionContext.Connection, interceptionContext)) || !Transactions.TryGetValue(transaction, out transactionRow)) { return; } Transactions.Remove(transaction); if (interceptionContext.Exception != null) { TransactionRow existingTransactionRow = null; var suspendedState = DbExecutionStrategy.Suspended; try { DbExecutionStrategy.Suspended = false; var executionStrategy = GetExecutionStrategy() ?? DbProviderServices.GetExecutionStrategy(interceptionContext.Connection); existingTransactionRow = TransactionContext.Transactions .AsNoTracking() .WithExecutionStrategy(executionStrategy) .SingleOrDefault(t => t.Id == transactionRow.Id); } catch (EntityCommandExecutionException) { // Error during verification, assume commit failed } finally { DbExecutionStrategy.Suspended = suspendedState; } if (existingTransactionRow != null) { // The transaction id is still in the database, so the commit succeeded interceptionContext.Exception = null; PruneTransactionHistory(transactionRow); } else { TransactionContext.Entry(transactionRow).State = EntityState.Detached; } } else { PruneTransactionHistory(transactionRow); } }
/// <summary>Stops tracking the transaction that was rolled back.</summary> /// <param name="transaction">The transaction that was rolled back.</param> /// <param name="interceptionContext">Contextual information associated with the call.</param> /// <seealso cref="M:System.Data.Entity.Infrastructure.Interception.IDbTransactionInterceptor.RolledBack(System.Data.Common.DbTransaction,System.Data.Entity.Infrastructure.Interception.DbTransactionInterceptionContext)" /> public override void RolledBack( DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { TransactionRow entity; if (this.TransactionContext == null || interceptionContext.Connection != null && !this.MatchesParentContext(interceptionContext.Connection, (DbInterceptionContext)interceptionContext) || !this._transactions.TryGetValue(transaction, out entity)) { return; } this._transactions.Remove(transaction); this.TransactionContext.Entry <TransactionRow>(entity).State = EntityState.Detached; }
public void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { var context = interceptionContext.DbContexts.FirstOrDefault(); if (context != null) { var sets = context.GetObjectContext().ObjectStateManager .GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified) .Select(x => x.EntitySet).Distinct().ToList(); sets.ForEach(x => QueryCacheManager.ExpireTag(QueryCacheManager.PrefixTagSet + x.Name)); } }
/// <summary> /// Stops tracking the transaction that was rolled back. /// </summary> /// <param name="transaction">The transaction that was rolled back.</param> /// <param name="interceptionContext">Contextual information associated with the call.</param> /// <seealso cref="IDbTransactionInterceptor.RolledBack" /> public override void RolledBack(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { TransactionRow transactionRow; if (TransactionContext == null || (interceptionContext.Connection != null && !MatchesParentContext(interceptionContext.Connection, interceptionContext)) || !Transactions.TryGetValue(transaction, out transactionRow)) { return; } Transactions.Remove(transaction); TransactionContext.Entry(transactionRow).State = EntityState.Detached; }
/// <summary> /// This method is called after <see cref="DbTransaction.Rollback" /> is invoked. /// The default implementation of this method filters by <see cref="DbContext" /> set into /// <see cref="Context" />, if any, and then logs the event. /// </summary> /// <param name="transaction">The transaction that was rolled back.</param> /// <param name="interceptionContext">Contextual information associated with the call.</param> public virtual void RolledBack(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (ShouldLog(interceptionContext)) { if (interceptionContext.Exception != null) { LogError(interceptionContext.Exception, "Failed to rollback transaction at {now} with error: {error}", DateTimeOffset.Now, interceptionContext.Exception.Message); } else { LogInformation("Rolled back transaction at {now}", DateTimeOffset.Now); } } }
public void Committed_does_not_wrap_exception_if_unknown_connection() { var context = Core.Objects.MockHelper.CreateMockObjectContext <object>(); var handler = new DefaultTransactionHandler(); handler.Initialize(context); var mockTransaction = new Mock <DbTransaction>().Object; var interceptionContext = new DbTransactionInterceptionContext().WithConnection(new Mock <DbConnection>().Object); interceptionContext.Exception = new Exception(); handler.Committed(mockTransaction, interceptionContext); Assert.IsNotType <CommitFailedException>(interceptionContext.Exception); }
/// <summary> /// This method is called after <see cref="DbTransaction.Commit" /> is invoked. /// The default implementation of this method filters by <see cref="DbContext" /> set into /// <see cref="Context" />, if any, and then logs the event. /// </summary> /// <param name="transaction">The transaction that was commited.</param> /// <param name="interceptionContext">Contextual information associated with the call.</param> public virtual void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (ShouldLog(interceptionContext)) { if (interceptionContext.Exception != null) { LogError( interceptionContext.Exception, $"Failed to commit transaction with error: {interceptionContext.Exception.Message}"); } else { LogInformation("Committed transaction {0}", transaction.GetHashCode()); } } }
/// <summary> /// Stops tracking the transaction that was rolled back. /// </summary> /// <param name="transaction">The transaction that was rolled back.</param> /// <param name="interceptionContext">Contextual information associated with the call.</param> /// <seealso cref="IDbTransactionInterceptor.RolledBack" /> public override void RolledBack(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (TransactionContext == null || !MatchesParentContext(transaction.Connection, interceptionContext)) { return; } TransactionRow transactionRow; if (_transactions.TryGetValue(transaction, out transactionRow)) { _transactions.Remove(transaction); TransactionContext.Transactions.Local.Remove(transactionRow); } }
/// <summary> /// If there was an exception thrown checks the database for this transaction and rethrows it if not found. /// Otherwise marks the commit as succeeded and queues the transaction information to be deleted. /// </summary> /// <param name="transaction">The transaction that was commited.</param> /// <param name="interceptionContext">Contextual information associated with the call.</param> /// <seealso cref="IDbTransactionInterceptor.Committed" /> public override void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { TransactionRow transactionRow; if (TransactionContext == null || (interceptionContext.Connection != null && !MatchesParentContext(interceptionContext.Connection, interceptionContext)) || !_transactions.TryGetValue(transaction, out transactionRow)) { return; } _transactions.Remove(transaction); if (interceptionContext.Exception != null) { TransactionRow existingTransactionRow = null; try { existingTransactionRow = TransactionContext.Transactions .AsNoTracking() .WithExecutionStrategy(new DefaultExecutionStrategy()) .SingleOrDefault(t => t.Id == transactionRow.Id); } catch (EntityCommandExecutionException) { // Transaction table doesn't exist } if (existingTransactionRow != null) { // The transaction id is still in the database, so the commit succeeded interceptionContext.Exception = null; PruneTransactionHistory(transactionRow); } else { TransactionContext.Entry(transactionRow).State = EntityState.Detached; } } else { PruneTransactionHistory(transactionRow); } }
public virtual void Committing(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (_timesToFail-- > 0) { if (ShouldRollBack) { transaction.Rollback(); } else { transaction.Commit(); } interceptionContext.Exception = new TimeoutException(); } else { _timesToFail = ShouldFailTimes; } }
/// <summary> /// If there was an exception thrown checks the database for this transaction and rethrows it if not found. /// Otherwise marks the commit as succeeded and queues the transaction information to be deleted. /// </summary> /// <param name="transaction">The transaction that was commited.</param> /// <param name="interceptionContext">Contextual information associated with the call.</param> /// <seealso cref="IDbTransactionInterceptor.Committed" /> public override void Committed(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (TransactionContext == null || !MatchesParentContext(transaction.Connection, interceptionContext)) { return; } TransactionRow transactionRow; if (_transactions.TryGetValue(transaction, out transactionRow)) { _transactions.Remove(transaction); if (interceptionContext.Exception != null) { var existingTransactionRow = TransactionContext.Transactions .AsNoTracking() .WithExecutionStrategy(new DefaultExecutionStrategy()) .SingleOrDefault(t => t.Id == transactionRow.Id); if (existingTransactionRow != null) { // The transaction id is still in the database, so the commit succeeded interceptionContext.Exception = null; PruneTransactionRows(transactionRow); } else { TransactionContext.Transactions.Local.Remove(transactionRow); } } else { PruneTransactionRows(transactionRow); } } else { Debug.Assert(false, "Expected the transaction to be registered"); } }
public virtual void Committing(DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { if (interceptionContext.DbContexts.Any(c => c is TransactionContext)) { return; } if (ShouldFailTimes-- > 0) { if (ShouldRollBack) { transaction.Rollback(); } else { transaction.Commit(); } interceptionContext.Exception = new TimeoutException(); } }
public void Committed_wraps_exceptions_for_known_transactions() { var context = Core.Objects.MockHelper.CreateMockObjectContext <object>(); var handler = new DefaultTransactionHandler(); handler.Initialize(context); var mockTransaction = new Mock <DbTransaction>().Object; var beginTransactionInterceptionContext = new BeginTransactionInterceptionContext(); beginTransactionInterceptionContext.Result = mockTransaction; handler.BeganTransaction(handler.Connection, beginTransactionInterceptionContext); var interceptionContext = new DbTransactionInterceptionContext().WithConnection(handler.Connection); interceptionContext.Exception = new Exception(); handler.Committed(mockTransaction, interceptionContext); Assert.IsType <CommitFailedException>(interceptionContext.Exception); }
public void BeganTransaction_does_not_record_transaction_if_connection_does_not_match() { var context = Core.Objects.MockHelper.CreateMockObjectContext <object>(); var handler = new DefaultTransactionHandler(); handler.Initialize(context); var mockTransaction = new Mock <DbTransaction>().Object; var beginTransactionInterceptionContext = new BeginTransactionInterceptionContext(); beginTransactionInterceptionContext.Result = mockTransaction; handler.BeganTransaction(new Mock <DbConnection>().Object, beginTransactionInterceptionContext); var interceptionContext = new DbTransactionInterceptionContext(); interceptionContext.Exception = new Exception(); handler.Committed(mockTransaction, interceptionContext); Assert.IsNotType <CommitFailedException>(interceptionContext.Exception); }
public void Committed_resolves_cache() { var mockTransactionHandler = new Mock <CacheTransactionHandler> { CallBase = true }; mockTransactionHandler.Protected() .Setup <ICache>("ResolveCache", ItExpr.IsAny <DbConnection>()) .Returns(Mock.Of <ICache>()); var dbConnection = Mock.Of <DbConnection>(); var interceptionContext = new DbTransactionInterceptionContext().WithConnection(dbConnection); var mockTransaction = new Mock <DbTransaction>(); mockTransaction.Protected().SetupGet <DbConnection>("DbConnection").Returns(dbConnection); var entitySets = new[] { "ES1" }; mockTransactionHandler.Object.InvalidateSets(mockTransaction.Object, entitySets, dbConnection); mockTransaction.Protected().SetupGet <DbConnection>("DbConnection").Returns((DbConnection)null); mockTransactionHandler.Object.Committed(mockTransaction.Object, interceptionContext); mockTransactionHandler.Protected() .Verify("ResolveCache", Times.Once(), dbConnection); }
/// <summary> /// If there was an exception thrown checks the database for this transaction and rethrows it if not found. /// Otherwise marks the commit as succeeded and queues the transaction information to be deleted. /// </summary> /// <param name="transaction">The transaction that was commited.</param> /// <param name="interceptionContext">Contextual information associated with the call.</param> /// <seealso cref="M:System.Data.Entity.Infrastructure.Interception.IDbTransactionInterceptor.Committed(System.Data.Common.DbTransaction,System.Data.Entity.Infrastructure.Interception.DbTransactionInterceptionContext)" /> public override void Committed( DbTransaction transaction, DbTransactionInterceptionContext interceptionContext) { TransactionRow transactionRow; if (this.TransactionContext == null || interceptionContext.Connection != null && !this.MatchesParentContext(interceptionContext.Connection, (DbInterceptionContext)interceptionContext) || !this._transactions.TryGetValue(transaction, out transactionRow)) { return; } this._transactions.Remove(transaction); if (interceptionContext.Exception != null) { TransactionRow transactionRow1 = (TransactionRow)null; try { transactionRow1 = this.TransactionContext.Transactions.AsNoTracking <TransactionRow>().WithExecutionStrategy <TransactionRow>((IDbExecutionStrategy) new DefaultExecutionStrategy()).SingleOrDefault <TransactionRow>((Expression <Func <TransactionRow, bool> >)(t => t.Id == transactionRow.Id)); } catch (EntityCommandExecutionException ex) { } if (transactionRow1 != null) { interceptionContext.Exception = (Exception)null; this.PruneTransactionHistory(transactionRow); } else { this.TransactionContext.Entry <TransactionRow>(transactionRow).State = EntityState.Detached; } } else { this.PruneTransactionHistory(transactionRow); } }