public TransactionScope(TransactionScopeOption option, TransactionScopeAsyncFlowOption asyncFlow) : this(option, TransactionManager.DefaultTimeout, asyncFlow) { }
public TransactionInterceptionHandler(TimeSpan timeout, TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Suppress, TransactionScopeOption scopeOption = TransactionScopeOption.Required, IsolationLevel isolationLevel = IsolationLevel.Unspecified) { this.asyncFlowOption = asyncFlowOption; this.scopeOption = scopeOption; this.timeout = timeout; this.isolationLevel = isolationLevel; }
public static TransactionScope New( TransactionScopeOption transactionScopeOption = TransactionScopeOption.Required, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted, TimeSpan?timeout = null, TransactionScopeAsyncFlowOption asyncFlowOptions = TransactionScopeAsyncFlowOption.Enabled ) { // NOTE: this is a fix, to avoid inner transaction timeout's to shorten the outer one... // // Since each transaction (even the nested one) has it's own timeout... // ... and the inner one completes with the outer one when using propagation... // ... we must set inner ones timeout to the outer ones (or enlarge inner one's timeout)... if (Transaction.Current != null && transactionScopeOption == TransactionScopeOption.Required) { timeout = TimeSpan.FromHours(6); } var options = new TransactionOptions { IsolationLevel = isolationLevel, Timeout = timeout ?? TimeSpan.FromMinutes(5) }; return(new TransactionScope(transactionScopeOption, options, asyncFlowOptions)); }
public TransactionScope( TransactionScopeOption scopeOption, TransactionScopeAsyncFlowOption asyncFlowOption ) { if ( !TransactionManager._platformValidated ) TransactionManager.ValidatePlatform(); if ( DiagnosticTrace.Verbose ) { MethodEnteredTraceRecord.Trace( SR.GetString( SR.TraceSourceBase ), "TransactionScope.ctor( TransactionScopeOption )" ); } ValidateAndSetAsyncFlowOption(asyncFlowOption); if ( NeedToCreateTransaction( scopeOption ) ) { committableTransaction = new CommittableTransaction(); expectedCurrent = committableTransaction.Clone(); } if ( DiagnosticTrace.Information ) { if ( null == expectedCurrent ) { TransactionScopeCreatedTraceRecord.Trace( SR.GetString( SR.TraceSourceBase ), TransactionTraceIdentifier.Empty, TransactionScopeResult.NoTransaction ); } else { TransactionScopeResult scopeResult; if ( null == committableTransaction ) { scopeResult = TransactionScopeResult.UsingExistingCurrent; } else { scopeResult = TransactionScopeResult.CreatedTransaction; } TransactionScopeCreatedTraceRecord.Trace( SR.GetString( SR.TraceSourceBase ), expectedCurrent.TransactionTraceId, scopeResult ); } } PushScope(); if ( DiagnosticTrace.Verbose ) { MethodExitedTraceRecord.Trace( SR.GetString( SR.TraceSourceBase ), "TransactionScope.ctor( TransactionScopeOption )" ); } }
/// <inheritdoc /> public void CreateTransactionScope( TransactionScopeOption txScopeOption, TransactionOptions txOptions, TransactionScopeAsyncFlowOption asyncFlowOption) { txScope = new TransactionScope(txScopeOption, txOptions, asyncFlowOption); }
void Initialize(TransactionScopeOption scopeOption, Transaction tx, TransactionOptions options, DTCOption interop, TimeSpan scopeTimeout, TransactionScopeAsyncFlowOption asyncFlow) { completed = false; isRoot = false; nested = 0; asyncFlowEnabled = asyncFlow == TransactionScopeAsyncFlowOption.Enabled; if (scopeTimeout < TimeSpan.Zero) { throw new ArgumentOutOfRangeException("scopeTimeout"); } this.timeout = scopeTimeout; oldTransaction = Transaction.CurrentInternal; Transaction.CurrentInternal = transaction = InitTransaction(tx, scopeOption, options); if (transaction != null) { transaction.InitScope(this); } if (parentScope != null) { parentScope.nested++; } }
public TransactionInterceptionAttribute(TimeSpan timeout, TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Suppress, TransactionScopeOption scopeOption = TransactionScopeOption.Required, IsolationLevel isolationLevel = IsolationLevel.Unspecified) : base(typeof(TransactionInterceptionHandler)) { this.AsyncFlowOption = asyncFlowOption; this.ScopeOption = scopeOption; this.Timeout = timeout; this.IsolationLevel = IsolationLevel; }
public FatQueueTransaction( IsolationLevel isolationLevel, TimeSpan? timeout, Func<IDbConnection> connectionFactory, TransactionScopeOption transactionScopeOption = TransactionScopeOption.Required, TransactionScopeAsyncFlowOption transactionScopeAsyncFlowOption = TransactionScopeAsyncFlowOption.Enabled) { _connectionFactory = connectionFactory; var transactionOptions = new TransactionOptions { IsolationLevel = isolationLevel, }; if (timeout != null) { transactionOptions.Timeout = timeout.Value; } if (System.Transactions.Transaction.Current == null || transactionScopeOption == TransactionScopeOption.RequiresNew) { _transaction = new CommittableTransaction(transactionOptions); _transactionScope = new TransactionScope(_transaction, transactionScopeAsyncFlowOption); } }
public static TransactionScope Acquire(TransactionScopeOption scope, IsolationLevel isolationLevel, TimeSpan timeout, TransactionScopeAsyncFlowOption asyncFlowOption) { TransactionOptions options = new TransactionOptions {IsolationLevel = isolationLevel, Timeout = timeout}; TransactionScope result = new TransactionScope(scope, options, asyncFlowOption); return result; }
/// <summary> /// Initializes a new instance of the TransactionScope class with the specified requirements. /// </summary> /// <param name="asyncFlowOption">Provides TransactionScopeAsyncFlowOption for creating a TransactionDecorator.</param> public TransactionDecorator(TransactionScopeAsyncFlowOption asyncFlowOption) { _asyncFlowOption = asyncFlowOption; if (_dataProvider != "System.Data.OleDb" && _useTransuctions.Enabled) { _scope = new TransactionScope(_asyncFlowOption); } }
private static TransactionScope CreateAsyncTransactionScope([CanBeNull] TimeSpan?transactionScopeTimeout = default(TimeSpan?)) { const TransactionScopeOption TransactionScopeOption = TransactionScopeOption.Required; const TransactionScopeAsyncFlowOption ScopeAsyncFlowOption = TransactionScopeAsyncFlowOption.Enabled; return(transactionScopeTimeout.HasValue ? new TransactionScope(TransactionScopeOption, transactionScopeTimeout.Value, ScopeAsyncFlowOption) : new TransactionScope(TransactionScopeOption, ScopeAsyncFlowOption)); }
public PostgreSqlDbTransaction(IsolationLevel isolationLevel, TimeSpan? timeout, Func<IDbConnection> connectionFactory, TransactionScopeOption transactionScopeOption = TransactionScopeOption.Required, TransactionScopeAsyncFlowOption transactionScopeAsyncFlowOption = TransactionScopeAsyncFlowOption.Enabled) : base(isolationLevel, timeout, connectionFactory, transactionScopeOption, transactionScopeAsyncFlowOption) { _isolationLevel = isolationLevel; }
/// <summary> /// 拦截方法 /// </summary> /// <param name="context">方法元数据</param> public void Advise(MethodAdviceContext context) { TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Enabled; using (TransactionScope scope = new TransactionScope(this._scopeOption, asyncFlowOption)) { context.Proceed(); scope.Complete(); } }
/// <summary> /// Defines the <see cref="SupportTransactionAttribute"/> with the specified arguments. /// </summary> /// <param name="transactionScopeOption">Options for creating transaction scope.</param> /// <param name="transactionScopeAsyncFlowOption">Options for creating transaction flow across thread.</param> /// <param name="isolationLevel">The isolation level of transaction.</param> /// <param name="timeOut">The timeout period for the transaction if necessary.</param> public SupportTransactionAttribute( TransactionScopeOption transactionScopeOption, TransactionScopeAsyncFlowOption transactionScopeAsyncFlowOption, IsolationLevel isolationLevel, int?timeOut) { TransactionScopeOption = transactionScopeOption; TransactionScopeAsyncFlowOption = transactionScopeAsyncFlowOption; IsolationLevel = isolationLevel; TimeOut = timeOut; }
// TODO: Make configurable private TransactionScope CreateTransactionScope( TransactionScopeOption transactionScopeOption = TransactionScopeOption.Required, TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Enabled, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) => new TransactionScope( transactionScopeOption, new TransactionOptions { IsolationLevel = isolationLevel }, asyncFlowOption);
public TransactionScope( TransactionScopeOption scopeOption, TransactionScopeAsyncFlowOption asyncFlowOption ) { TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log; if (etwLog.IsEnabled()) { etwLog.MethodEnter(TraceSourceType.TraceSourceBase, this); } ValidateAndSetAsyncFlowOption(asyncFlowOption); if (NeedToCreateTransaction(scopeOption)) { _committableTransaction = new CommittableTransaction(); _expectedCurrent = _committableTransaction.Clone(); } if (null == _expectedCurrent) { if (etwLog.IsEnabled()) { etwLog.TransactionScopeCreated(TransactionTraceIdentifier.Empty, TransactionScopeResult.NoTransaction); } } else { TransactionScopeResult scopeResult; if (null == _committableTransaction) { scopeResult = TransactionScopeResult.UsingExistingCurrent; } else { scopeResult = TransactionScopeResult.CreatedTransaction; } if (etwLog.IsEnabled()) { etwLog.TransactionScopeCreated(_expectedCurrent.TransactionTraceId, scopeResult); } } PushScope(); if (etwLog.IsEnabled()) { etwLog.MethodExit(TraceSourceType.TraceSourceBase, this); } }
private void ValidateAndSetAsyncFlowOption(TransactionScopeAsyncFlowOption asyncFlowOption) { if (asyncFlowOption < TransactionScopeAsyncFlowOption.Suppress || asyncFlowOption > TransactionScopeAsyncFlowOption.Enabled) { throw new ArgumentOutOfRangeException(nameof(asyncFlowOption)); } if (asyncFlowOption == TransactionScopeAsyncFlowOption.Enabled) { AsyncFlowEnabled = true; } }
private static TransactionScope CreateTransactionScope( TimeSpan?timeout, IsolationLevel isolationLevel, TransactionScopeAsyncFlowOption asyncOption = TransactionScopeAsyncFlowOption.Suppress) { timeout ??= DefaultTimeout; var transactionOptions = new TransactionOptions { IsolationLevel = isolationLevel, Timeout = timeout.Value }; return(new TransactionScope(TransactionScopeOption.Required, transactionOptions, asyncOption)); }
public static TransactionScope CreateSerializable(TransactionScopeOption transactionScopeOption, TimeSpan? timeout, TransactionScopeAsyncFlowOption transactionScopeAsyncFlowOption) { var transactionOptions = new TransactionOptions { IsolationLevel = IsolationLevel.Serializable, }; if (timeout.HasValue) { transactionOptions.Timeout = timeout.Value; } return new TransactionScope(transactionScopeOption, transactionOptions, transactionScopeAsyncFlowOption); }
/// <summary> /// Marks a method to run within the specified /// type of transactional context. /// </summary> /// <param name="transactionType"> /// Specifies the transactional context within which the /// method should run.</param> /// <param name="transactionIsolationLevel"> /// Specifies override for transaction isolation level. /// Default can be specified in .config file via CslaTransactionIsolationLevel setting /// If none specified, Serializable level is used /// </param> /// <param name="asyncFlowOption"> /// Specifies the async flow option used to initialize /// the transaction. /// </param> public TransactionalAttribute( TransactionalTypes transactionType, TransactionIsolationLevel transactionIsolationLevel, TransactionScopeAsyncFlowOption asyncFlowOption) { TransactionType = transactionType; TransactionIsolationLevel = transactionIsolationLevel; #if NETSTANDARD2_0 || NET5_0 || NET6_0 if (transactionType == TransactionalTypes.TransactionScope) #else if (transactionType == TransactionalTypes.TransactionScope || transactionType == TransactionalTypes.EnterpriseServices) #endif { TimeoutInSeconds = ApplicationContext.DefaultTransactionTimeoutInSeconds; } AsyncFlowOption = asyncFlowOption; }
public virtual async Task <TResult> ExecuteAsync <TResult>(Func <TResult> map, TransactionScopeOption scopeOption = TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Enabled, CancellationToken cancellationToken = default(CancellationToken), [CallerMemberName] string method = null) { // i'm honestly not lovin this method. looks like it could cause // too many holes and could lead to some seriously, serious // nasty, nasty. using (var scope = new TransactionScope(scopeOption, TransactionScopeAsyncFlowOption.Enabled)) { // could also be abused as sync over async. // todo: write tests to prove abuse and write about // why it's bad, bad, bad. var result = await Task.FromResult(map()); scope.Complete(); return(result); } }
public static TransactionScope CreateRepeatableRead(TransactionScopeOption transactionScopeOption, TimeSpan? timeout, TransactionScopeAsyncFlowOption transactionScopeAsyncFlowOption) { var transactionOptions = new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead, }; if (transactionScopeOption == TransactionScopeOption.Required) { var currentTransaction = Transaction.Current; if (currentTransaction != null && currentTransaction.IsolationLevel == IsolationLevel.Serializable) { transactionOptions.IsolationLevel = currentTransaction.IsolationLevel; } } if (timeout.HasValue) { transactionOptions.Timeout = timeout.Value; } return new TransactionScope(transactionScopeOption, transactionOptions, transactionScopeAsyncFlowOption); }
public TransactionScope( Transaction transactionToUse, TimeSpan scopeTimeout, TransactionScopeAsyncFlowOption asyncFlowOption) { TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log; if (etwLog.IsEnabled()) { etwLog.MethodEnter(TraceSourceType.TraceSourceBase, this); } ValidateAndSetAsyncFlowOption(asyncFlowOption); Initialize( transactionToUse, scopeTimeout, false); if (etwLog.IsEnabled()) { etwLog.MethodExit(TraceSourceType.TraceSourceBase, this); } }
/// <summary> /// UnitOfWork联合提交(包含处理领域事件)异步扩展方法 /// </summary> /// <param name="unitOfWork">工作单元实例</param> public static async Task UnitedCommitAsync(this IUnitOfWork unitOfWork) { TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Enabled; try { //开启事务 using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, asyncFlowOption)) { //提交工作单元 await unitOfWork.CommitAsync(); //处理领域事件 await EventMediator.HandleUncompletedEventsAsync(); //事务完成 scope.Complete(); } } catch { //不参与事务 using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress, asyncFlowOption)) { //回滚工作单元 unitOfWork.RollBack(); //清空未处理的领域事件 EventMediator.ClearUncompletedEvents(); //事务完成 scope.Complete(); } throw; } }
public TransactionScope(Transaction transactionToUse, TimeSpan scopeTimeout, TransactionScopeAsyncFlowOption asyncFlowOption) { throw new NotImplementedException(); }
/// <summary> /// 创建 TransactionScope 对象实例 /// </summary> /// <param name="scopeOption">事务范围复选项</param> /// <param name="isolationLevel">隔离级别</param> /// <param name="asyncFlowOption">是否支持异步</param> /// <returns>事务性范围对象</returns> private static TransactionScope Create(TransactionScopeOption scopeOption, IsolationLevel isolationLevel, TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Suppress) { return Create(scopeOption, TransactionScopeFactory.Timeout, isolationLevel, asyncFlowOption); }
/// <summary> /// 创建 TransactionScope 对象实例 /// 由于 Transaction 自带交易隔离度说明,因此不涉及从配置提取该参数的操作 /// </summary> /// <param name="transactionToUse">事务</param> /// <param name="scopeTimeout">超时范围</param> /// <param name="asyncFlowOption">是否支持异步</param> /// <returns>事务性范围对象</returns> private static TransactionScope Create(Transaction transactionToUse, TimeSpan scopeTimeout, TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Suppress) { if (scopeTimeout < TimeSpan.Zero) throw new ArgumentOutOfRangeException("scopeTimeout"); return new TransactionScope(transactionToUse, scopeTimeout, asyncFlowOption); }
public TransactionScope CreateTransaction(System.Transactions.IsolationLevel isolationLevel, int timeoutInSeconds, TransactionScopeAsyncFlowOption asyncFlowOption) { var option = TransactionScopeOption.Required; var options = new TransactionOptions() { IsolationLevel = isolationLevel, Timeout = new TimeSpan(0, 0, timeoutInSeconds) }; return(new TransactionScope(option, options, asyncFlowOption)); }
public TransactionScope( TransactionScopeOption scopeOption, TransactionScopeAsyncFlowOption asyncFlowOption ) { TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log; if (etwLog.IsEnabled()) { etwLog.MethodEnter(TraceSourceType.TraceSourceBase, this); } ValidateAndSetAsyncFlowOption(asyncFlowOption); if (NeedToCreateTransaction(scopeOption)) { _committableTransaction = new CommittableTransaction(); _expectedCurrent = _committableTransaction.Clone(); } if (null == _expectedCurrent) { if (etwLog.IsEnabled()) { etwLog.TransactionScopeCreated(TransactionTraceIdentifier.Empty, TransactionScopeResult.NoTransaction); } } else { TransactionScopeResult scopeResult; if (null == _committableTransaction) { scopeResult = TransactionScopeResult.UsingExistingCurrent; } else { scopeResult = TransactionScopeResult.CreatedTransaction; } if (etwLog.IsEnabled()) { etwLog.TransactionScopeCreated(_expectedCurrent.TransactionTraceId, scopeResult); } } PushScope(); if (etwLog.IsEnabled()) { etwLog.MethodExit(TraceSourceType.TraceSourceBase, this); } }
public TransactionScope(Transaction transactionToUse, TransactionScopeAsyncFlowOption asyncFlowOption) { }
public void VerifyBYOT(TransactionScopeAsyncFlowOption asyncFlowOption) { string txId1; string txId2; string txId3; AssertTransactionNull(); CommittableTransaction tx = new CommittableTransaction(); Transaction.Current = tx; txId1 = AssertAndGetCurrentTransactionId(); using (TransactionScope scope = new TransactionScope(asyncFlowOption)) { txId2 = AssertAndGetCurrentTransactionId(); scope.Complete(); } txId3 = AssertAndGetCurrentTransactionId(); Transaction.Current = null; tx.Commit(); Assert.Equal(txId1, txId2); Assert.Equal(txId2, txId3); AssertTransactionNull(); }
public void DoTxNewThread(TransactionScopeAsyncFlowOption asyncFlowOption) { string txId1; string txId2; ManualResetEvent waitCompletion = new ManualResetEvent(false); AssertTransactionNull(); using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, asyncFlowOption)) { txId1 = AssertAndGetCurrentTransactionId(); ThreadSyncObject context = new ThreadSyncObject() { Id = txId1, RootAsyncFlowOption = asyncFlowOption }; Task.Factory.StartNew(DoTxWork, context, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Wait(); txId2 = AssertAndGetCurrentTransactionId(); scope.Complete(); } AssertTransactionNull(); Assert.Equal(txId1, txId2); }
public TransactionScope( Transaction transactionToUse, TransactionScopeAsyncFlowOption asyncFlowOption ) { if ( !TransactionManager._platformValidated ) TransactionManager.ValidatePlatform(); if ( DiagnosticTrace.Verbose ) { MethodEnteredTraceRecord.Trace( SR.GetString( SR.TraceSourceBase ), "TransactionScope.ctor( Transaction )" ); } ValidateAndSetAsyncFlowOption(asyncFlowOption); Initialize( transactionToUse, TimeSpan.Zero, false ); if ( DiagnosticTrace.Verbose ) { MethodExitedTraceRecord.Trace( SR.GetString( SR.TraceSourceBase ), "TransactionScope.ctor( Transaction )" ); } }
public TransactionScope( TransactionScopeOption scopeOption, TimeSpan scopeTimeout, TransactionScopeAsyncFlowOption asyncFlowOption ) { if ( !TransactionManager._platformValidated ) TransactionManager.ValidatePlatform(); if ( DiagnosticTrace.Verbose ) { MethodEnteredTraceRecord.Trace( SR.GetString( SR.TraceSourceBase ), "TransactionScope.ctor( TransactionScopeOption, TimeSpan )" ); } ValidateScopeTimeout( "scopeTimeout", scopeTimeout ); TimeSpan txTimeout = TransactionManager.ValidateTimeout( scopeTimeout ); ValidateAndSetAsyncFlowOption(asyncFlowOption); if ( NeedToCreateTransaction( scopeOption )) { this.committableTransaction = new CommittableTransaction( txTimeout ); this.expectedCurrent = committableTransaction.Clone(); } if ( (null != this.expectedCurrent) && (null == this.committableTransaction) && (TimeSpan.Zero != scopeTimeout) ) { // scopeTimer = new Timer( TransactionScope.TimerCallback, this, scopeTimeout, TimeSpan.Zero ); } if ( DiagnosticTrace.Information ) { if ( null == expectedCurrent ) { TransactionScopeCreatedTraceRecord.Trace( SR.GetString( SR.TraceSourceBase ), TransactionTraceIdentifier.Empty, TransactionScopeResult.NoTransaction ); } else { TransactionScopeResult scopeResult; if ( null == committableTransaction ) { scopeResult = TransactionScopeResult.UsingExistingCurrent; } else { scopeResult = TransactionScopeResult.CreatedTransaction; } TransactionScopeCreatedTraceRecord.Trace( SR.GetString( SR.TraceSourceBase ), expectedCurrent.TransactionTraceId, scopeResult ); } } PushScope(); if ( DiagnosticTrace.Verbose ) { MethodExitedTraceRecord.Trace( SR.GetString( SR.TraceSourceBase ), "TransactionScope.ctor( TransactionScopeOption, TimeSpan )" ); } }
public void VerifyNestedTxScope(TransactionScopeOption parentScopeOption, TransactionScopeAsyncFlowOption parentAsyncFlowOption, TransactionScopeOption childScopeOption, TransactionScopeAsyncFlowOption childAsyncFlowOption) { string txId1; string txId2; string txId3; AssertTransactionNull(); using (TransactionScope parent = new TransactionScope(parentScopeOption, parentAsyncFlowOption)) { txId1 = AssertAndGetCurrentTransactionId(parentScopeOption); using (TransactionScope child = new TransactionScope(childScopeOption, childAsyncFlowOption)) { txId2 = AssertAndGetCurrentTransactionId(childScopeOption); child.Complete(); } txId3 = AssertAndGetCurrentTransactionId(parentScopeOption); parent.Complete(); } AssertTransactionNull(); Assert.Equal(txId1, txId3); switch (childScopeOption) { case TransactionScopeOption.Required: if (parentScopeOption == TransactionScopeOption.Suppress) { Assert.NotEqual(txId1, txId2); } else { Assert.Equal(txId1, txId2); } break; case TransactionScopeOption.RequiresNew: Assert.NotEqual(txId1, txId2); break; case TransactionScopeOption.Suppress: if (parentScopeOption == TransactionScopeOption.Suppress) { Assert.Equal(txId1, txId2); } else { Assert.NotEqual(txId1, txId2); } break; } }
public TransactionScope(TransactionScopeAsyncFlowOption asyncFlowOption) { }
public TransactionScope(Transaction transactionToUse, TransactionScopeAsyncFlowOption asyncFlowOption) { }
/// <summary> /// 创建 TransactionScope 对象实例 /// </summary> /// <param name="scopeOption">事务范围复选项</param> /// <param name="scopeTimeout">超时时间</param> /// <param name="isolationLevel">隔离级别</param> /// <param name="asyncFlowOption">是否支持异步</param> /// <returns>事务性范围对象</returns> public static TransactionScope Create(TransactionScopeOption scopeOption, TimeSpan scopeTimeout, IsolationLevel isolationLevel, TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Suppress) { if (scopeTimeout <= TimeSpan.Zero) throw new ArgumentOutOfRangeException("scopeTimeout"); // 组装TS参数 TransactionOptions options = new TransactionOptions(); options.Timeout = scopeTimeout; options.IsolationLevel = isolationLevel; return Create(scopeOption, options, asyncFlowOption); }
public TransactionScope(TransactionScopeOption option, TransactionScopeAsyncFlowOption asyncFlow) : this(option, TransactionManager.DefaultTimeout, asyncFlow) { }
public TransactionScope (TransactionScopeOption option, TimeSpan timeout, TransactionScopeAsyncFlowOption asyncFlow) { Initialize (option, null, defaultOptions, DTCOption.None, timeout, asyncFlow); }
public void VerifyParentTwoChildTest(TransactionScopeAsyncFlowOption parentAsyncFlowOption, TransactionScopeOption child1ScopeOption, TransactionScopeAsyncFlowOption child1AsyncFlowOption, TransactionScopeOption child2ScopeOption, TransactionScopeAsyncFlowOption child2AsyncFlowOption) { string txId1; string txId2; string txId3; string txId4; string txId5; AssertTransactionNull(); using (TransactionScope parent = new TransactionScope(parentAsyncFlowOption)) { txId1 = AssertAndGetCurrentTransactionId(); using (TransactionScope child1 = new TransactionScope(child1ScopeOption, child1AsyncFlowOption)) { txId2 = AssertAndGetCurrentTransactionId(child1ScopeOption); child1.Complete(); } txId3 = AssertAndGetCurrentTransactionId(); using (TransactionScope child2 = new TransactionScope(child2ScopeOption, child2AsyncFlowOption)) { txId4 = AssertAndGetCurrentTransactionId(child2ScopeOption); child2.Complete(); } txId5 = AssertAndGetCurrentTransactionId(); parent.Complete(); } AssertTransactionNull(); Assert.Equal(txId1, txId3); Assert.Equal(txId3, txId5); if (child1ScopeOption == TransactionScopeOption.Required) { Assert.Equal(txId1, txId2); } else { Assert.NotEqual(txId1, txId2); if (child1ScopeOption == TransactionScopeOption.Suppress) { Assert.Equal(null, txId2); } } if (child2ScopeOption == TransactionScopeOption.Required) { Assert.Equal(txId1, txId4); } else { Assert.NotEqual(txId1, txId4); if (child2ScopeOption == TransactionScopeOption.Suppress) { Assert.Equal(null, txId4); } } }
public TransactionScope( TransactionScopeOption scopeOption, TimeSpan scopeTimeout, TransactionScopeAsyncFlowOption asyncFlowOption ) { if (DiagnosticTrace.Verbose) { MethodEnteredTraceRecord.Trace(SR.TraceSourceBase, "TransactionScope.ctor( TransactionScopeOption, TimeSpan )"); } ValidateScopeTimeout(nameof(scopeTimeout), scopeTimeout); TimeSpan txTimeout = TransactionManager.ValidateTimeout(scopeTimeout); ValidateAndSetAsyncFlowOption(asyncFlowOption); if (NeedToCreateTransaction(scopeOption)) { _committableTransaction = new CommittableTransaction(txTimeout); _expectedCurrent = _committableTransaction.Clone(); } if ((null != _expectedCurrent) && (null == _committableTransaction) && (TimeSpan.Zero != scopeTimeout)) { // BUGBUG: Scopes should not use individual timers _scopeTimer = new Timer( TimerCallback, this, scopeTimeout, TimeSpan.Zero); } if (DiagnosticTrace.Information) { if (null == _expectedCurrent) { TransactionScopeCreatedTraceRecord.Trace(SR.TraceSourceBase, TransactionTraceIdentifier.Empty, TransactionScopeResult.NoTransaction); } else { TransactionScopeResult scopeResult; if (null == _committableTransaction) { scopeResult = TransactionScopeResult.UsingExistingCurrent; } else { scopeResult = TransactionScopeResult.CreatedTransaction; } TransactionScopeCreatedTraceRecord.Trace(SR.TraceSourceBase, _expectedCurrent.TransactionTraceId, scopeResult); } } PushScope(); if (DiagnosticTrace.Verbose) { MethodExitedTraceRecord.Trace(SR.TraceSourceBase, "TransactionScope.ctor( TransactionScopeOption, TimeSpan )"); } }
/// <summary> /// Sets the default transaction async flow option /// used to create new TransactionScope objects. /// </summary> /// <param name="asyncFlowOption">Async flow option</param> public DataOptions DefaultTransactionAsyncFlowOption(TransactionScopeAsyncFlowOption asyncFlowOption) { ApplicationContext.DefaultTransactionAsyncFlowOption = asyncFlowOption; return(this); }
private void ValidateAndSetAsyncFlowOption(TransactionScopeAsyncFlowOption asyncFlowOption) { if (asyncFlowOption < TransactionScopeAsyncFlowOption.Suppress || asyncFlowOption > TransactionScopeAsyncFlowOption.Enabled) { throw new ArgumentOutOfRangeException(nameof(asyncFlowOption)); } if (asyncFlowOption == TransactionScopeAsyncFlowOption.Enabled) { AsyncFlowEnabled = true; } }
public TransactionScope( TransactionScopeOption scopeOption, TimeSpan scopeTimeout, TransactionScopeAsyncFlowOption asyncFlowOption ) { TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log; if (etwLog.IsEnabled()) { etwLog.MethodEnter(TraceSourceType.TraceSourceBase, this); } ValidateScopeTimeout(nameof(scopeTimeout), scopeTimeout); TimeSpan txTimeout = TransactionManager.ValidateTimeout(scopeTimeout); ValidateAndSetAsyncFlowOption(asyncFlowOption); if (NeedToCreateTransaction(scopeOption)) { _committableTransaction = new CommittableTransaction(txTimeout); _expectedCurrent = _committableTransaction.Clone(); } if ((null != _expectedCurrent) && (null == _committableTransaction) && (TimeSpan.Zero != scopeTimeout)) { // BUGBUG: Scopes should not use individual timers _scopeTimer = new Timer( TimerCallback, this, scopeTimeout, TimeSpan.Zero); } if (null == _expectedCurrent) { if (etwLog.IsEnabled()) { etwLog.TransactionScopeCreated(TransactionTraceIdentifier.Empty, TransactionScopeResult.NoTransaction); } } else { TransactionScopeResult scopeResult; if (null == _committableTransaction) { scopeResult = TransactionScopeResult.UsingExistingCurrent; } else { scopeResult = TransactionScopeResult.CreatedTransaction; } if (etwLog.IsEnabled()) { etwLog.TransactionScopeCreated(_expectedCurrent.TransactionTraceId, scopeResult); } } PushScope(); if (etwLog.IsEnabled()) { etwLog.MethodExit(TraceSourceType.TraceSourceBase, this); } }
public TransactionScope( TransactionScopeOption scopeOption, TransactionOptions transactionOptions, TransactionScopeAsyncFlowOption asyncFlowOption ) { if (DiagnosticTrace.Verbose) { MethodEnteredTraceRecord.Trace(SR.TraceSourceBase, "TransactionScope.ctor( TransactionScopeOption, TransactionOptions )"); } ValidateScopeTimeout("transactionOptions.Timeout", transactionOptions.Timeout); TimeSpan scopeTimeout = transactionOptions.Timeout; transactionOptions.Timeout = TransactionManager.ValidateTimeout(transactionOptions.Timeout); TransactionManager.ValidateIsolationLevel(transactionOptions.IsolationLevel); ValidateAndSetAsyncFlowOption(asyncFlowOption); if (NeedToCreateTransaction(scopeOption)) { _committableTransaction = new CommittableTransaction(transactionOptions); _expectedCurrent = _committableTransaction.Clone(); } else { if (null != _expectedCurrent) { // If the requested IsolationLevel is stronger than that of the specified transaction, throw. if ((IsolationLevel.Unspecified != transactionOptions.IsolationLevel) && (_expectedCurrent.IsolationLevel != transactionOptions.IsolationLevel)) { throw new ArgumentException(SR.TransactionScopeIsolationLevelDifferentFromTransaction, "transactionOptions.IsolationLevel"); } } } if ((null != _expectedCurrent) && (null == _committableTransaction) && (TimeSpan.Zero != scopeTimeout)) { // BUGBUG: Scopes should use a shared timer _scopeTimer = new Timer( TimerCallback, this, scopeTimeout, TimeSpan.Zero); } if (DiagnosticTrace.Information) { if (null == _expectedCurrent) { TransactionScopeCreatedTraceRecord.Trace(SR.TraceSourceBase, TransactionTraceIdentifier.Empty, TransactionScopeResult.NoTransaction); } else { TransactionScopeResult scopeResult; if (null == _committableTransaction) { scopeResult = TransactionScopeResult.UsingExistingCurrent; } else { scopeResult = TransactionScopeResult.CreatedTransaction; } TransactionScopeCreatedTraceRecord.Trace(SR.TraceSourceBase, _expectedCurrent.TransactionTraceId, scopeResult); } } PushScope(); if (DiagnosticTrace.Verbose) { MethodExitedTraceRecord.Trace(SR.TraceSourceBase, "TransactionScope.ctor( TransactionScopeOption, TransactionOptions )"); } }
public TransactionScope( TransactionScopeOption scopeOption, TransactionOptions transactionOptions, TransactionScopeAsyncFlowOption asyncFlowOption ) { TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log; if (etwLog.IsEnabled()) { etwLog.MethodEnter(TraceSourceType.TraceSourceBase, this); } ValidateScopeTimeout("transactionOptions.Timeout", transactionOptions.Timeout); TimeSpan scopeTimeout = transactionOptions.Timeout; transactionOptions.Timeout = TransactionManager.ValidateTimeout(transactionOptions.Timeout); TransactionManager.ValidateIsolationLevel(transactionOptions.IsolationLevel); ValidateAndSetAsyncFlowOption(asyncFlowOption); if (NeedToCreateTransaction(scopeOption)) { _committableTransaction = new CommittableTransaction(transactionOptions); _expectedCurrent = _committableTransaction.Clone(); } else { if (null != _expectedCurrent) { // If the requested IsolationLevel is stronger than that of the specified transaction, throw. if ((IsolationLevel.Unspecified != transactionOptions.IsolationLevel) && (_expectedCurrent.IsolationLevel != transactionOptions.IsolationLevel)) { throw new ArgumentException(SR.TransactionScopeIsolationLevelDifferentFromTransaction, "transactionOptions.IsolationLevel"); } } } if ((null != _expectedCurrent) && (null == _committableTransaction) && (TimeSpan.Zero != scopeTimeout)) { // BUGBUG: Scopes should use a shared timer _scopeTimer = new Timer( TimerCallback, this, scopeTimeout, TimeSpan.Zero); } if (null == _expectedCurrent) { if (etwLog.IsEnabled()) { etwLog.TransactionScopeCreated(TransactionTraceIdentifier.Empty, TransactionScopeResult.NoTransaction); } } else { TransactionScopeResult scopeResult; if (null == _committableTransaction) { scopeResult = TransactionScopeResult.UsingExistingCurrent; } else { scopeResult = TransactionScopeResult.CreatedTransaction; } if (etwLog.IsEnabled()) { etwLog.TransactionScopeCreated(_expectedCurrent.TransactionTraceId, scopeResult); } } PushScope(); if (etwLog.IsEnabled()) { etwLog.MethodExit(TraceSourceType.TraceSourceBase, this); } }
public TransactionScope( Transaction transactionToUse, TimeSpan scopeTimeout, TransactionScopeAsyncFlowOption asyncFlowOption) { if (DiagnosticTrace.Verbose) { MethodEnteredTraceRecord.Trace(SR.TraceSourceBase, "TransactionScope.ctor( Transaction, TimeSpan )"); } ValidateAndSetAsyncFlowOption(asyncFlowOption); Initialize( transactionToUse, scopeTimeout, false); if (DiagnosticTrace.Verbose) { MethodExitedTraceRecord.Trace(SR.TraceSourceBase, "TransactionScope.ctor( Transaction, TimeSpan )"); } }
public TransactionScope(TransactionScopeAsyncFlowOption asyncFlowOption) : this(TransactionScopeOption.Required, asyncFlowOption) { }
public TransactionScope(TransactionScopeAsyncFlowOption asyncFlowOption) : this(TransactionScopeOption.Required, asyncFlowOption) { }
/// <summary> /// 创建 TransactionScope 对象实例 /// </summary> /// <param name="scopeOption">事务复选项</param> /// <param name="transactionOptions">事务附加信息</param> /// <param name="asyncFlowOption">是否支持异步</param> /// <returns>事务性范围对象</returns> private static TransactionScope Create(TransactionScopeOption scopeOption, TransactionOptions transactionOptions, TransactionScopeAsyncFlowOption asyncFlowOption = TransactionScopeAsyncFlowOption.Suppress) { return new TransactionScope(scopeOption, transactionOptions, asyncFlowOption); }
void Initialize (TransactionScopeOption scopeOption, Transaction tx, TransactionOptions options, DTCOption interop, TimeSpan timeout, TransactionScopeAsyncFlowOption asyncFlow) { completed = false; isRoot = false; nested = 0; asyncFlowEnabled = asyncFlow == TransactionScopeAsyncFlowOption.Enabled; if (timeout < TimeSpan.Zero) throw new ArgumentOutOfRangeException ("timeout"); this.timeout = timeout; oldTransaction = Transaction.CurrentInternal; Transaction.CurrentInternal = transaction = InitTransaction (tx, scopeOption); if (transaction != null) transaction.InitScope (this); if (parentScope != null) parentScope.nested ++; }
public TransactionScope(TransactionScopeOption scopeOption, TransactionOptions transactionOptions, TransactionScopeAsyncFlowOption asyncFlowOption) { throw new NotImplementedException(); }
public TransactionScope( Transaction transactionToUse, TimeSpan scopeTimeout, TransactionScopeAsyncFlowOption asyncFlowOption) { TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log; if (etwLog.IsEnabled()) { etwLog.MethodEnter(TraceSourceType.TraceSourceBase, this); } ValidateAndSetAsyncFlowOption(asyncFlowOption); Initialize( transactionToUse, scopeTimeout, false); if (etwLog.IsEnabled()) { etwLog.MethodExit(TraceSourceType.TraceSourceBase, this); } }
public TransactionScope(TransactionScopeAsyncFlowOption asyncFlowOption) : this(TransactionScopeOption.Required, TransactionManager.DefaultTimeout, asyncFlowOption) { }
internal RealTransactionScope(TransactionScopeOption scopeOption, TransactionOptions transactionOptions, TransactionScopeAsyncFlowOption asyncFlowOption) { _transactionOptions = transactionOptions; _inner = new System.Transactions.TransactionScope(scopeOption, transactionOptions, asyncFlowOption); }
public TransactionScope(TransactionScopeOption scopeOption, TimeSpan scopeTimeout, TransactionScopeAsyncFlowOption asyncFlow) { Initialize(scopeOption, null, defaultOptions, DTCOption.None, scopeTimeout, asyncFlow); }
public TransactionScope( TransactionScopeOption scopeOption, TimeSpan scopeTimeout, TransactionScopeAsyncFlowOption asyncFlowOption ) { TransactionsEtwProvider etwLog = TransactionsEtwProvider.Log; if (etwLog.IsEnabled()) { etwLog.MethodEnter(TraceSourceType.TraceSourceBase, this); } ValidateScopeTimeout(nameof(scopeTimeout), scopeTimeout); TimeSpan txTimeout = TransactionManager.ValidateTimeout(scopeTimeout); ValidateAndSetAsyncFlowOption(asyncFlowOption); if (NeedToCreateTransaction(scopeOption)) { _committableTransaction = new CommittableTransaction(txTimeout); _expectedCurrent = _committableTransaction.Clone(); } if ((null != _expectedCurrent) && (null == _committableTransaction) && (TimeSpan.Zero != scopeTimeout)) { // BUGBUG: Scopes should not use individual timers _scopeTimer = new Timer( TimerCallback, this, scopeTimeout, TimeSpan.Zero); } if (null == _expectedCurrent) { if (etwLog.IsEnabled()) { etwLog.TransactionScopeCreated(TransactionTraceIdentifier.Empty, TransactionScopeResult.NoTransaction); } } else { TransactionScopeResult scopeResult; if (null == _committableTransaction) { scopeResult = TransactionScopeResult.UsingExistingCurrent; } else { scopeResult = TransactionScopeResult.CreatedTransaction; } if (etwLog.IsEnabled()) { etwLog.TransactionScopeCreated(_expectedCurrent.TransactionTraceId, scopeResult); } } PushScope(); if (etwLog.IsEnabled()) { etwLog.MethodExit(TraceSourceType.TraceSourceBase, this); } }
public TransactionScope(TransactionScopeAsyncFlowOption asyncFlowOption) { }
public void VerifyThreeTxScope(TransactionScopeAsyncFlowOption asyncFlowOption1, TransactionScopeAsyncFlowOption asyncFlowOption2, TransactionScopeAsyncFlowOption asyncFlowOption3, TransactionScopeOption scopeOption, bool nested) { string txId1; string txId2; string txId3; string txId4; string txId5; AssertTransactionNull(); if (nested) { using (TransactionScope scope1 = new TransactionScope(scopeOption, asyncFlowOption1)) { txId1 = AssertAndGetCurrentTransactionId(scopeOption); using (TransactionScope scope2 = new TransactionScope(scopeOption, asyncFlowOption2)) { txId2 = AssertAndGetCurrentTransactionId(scopeOption); using (TransactionScope scope3 = new TransactionScope(scopeOption, asyncFlowOption3)) { txId3 = AssertAndGetCurrentTransactionId(scopeOption); scope3.Complete(); } txId4 = AssertAndGetCurrentTransactionId(scopeOption); scope2.Complete(); } txId5 = AssertAndGetCurrentTransactionId(scopeOption); scope1.Complete(); } AssertTransactionNull(); Assert.Equal(txId1, txId5); Assert.Equal(txId2, txId4); if (scopeOption == TransactionScopeOption.RequiresNew) { Assert.NotEqual(txId1, txId2); Assert.NotEqual(txId2, txId3); } else { Assert.Equal(txId1, txId2); Assert.Equal(txId2, txId3); } } else { using (TransactionScope scope1 = new TransactionScope(scopeOption, asyncFlowOption1)) { txId1 = AssertAndGetCurrentTransactionId(scopeOption); scope1.Complete(); } AssertTransactionNull(); using (TransactionScope scope2 = new TransactionScope(scopeOption, asyncFlowOption2)) { txId2 = AssertAndGetCurrentTransactionId(scopeOption); scope2.Complete(); } AssertTransactionNull(); using (TransactionScope scope3 = new TransactionScope(scopeOption, asyncFlowOption3)) { txId3 = AssertAndGetCurrentTransactionId(scopeOption); scope3.Complete(); } if (scopeOption == TransactionScopeOption.Suppress) { Assert.Equal(null, txId1); Assert.Equal(txId1, txId2); Assert.Equal(txId2, txId3); } else { Assert.NotEqual(txId1, txId2); Assert.NotEqual(txId2, txId3); } } AssertTransactionNull(); }