Windows-powershell / PowerShell-master /src /Microsoft.PowerShell.Commands.Management /commands /management /StartTransactionCommand.cs
| // Copyright (c) Microsoft Corporation. | |
| // Licensed under the MIT License. | |
| using System; | |
| using System.Management.Automation; | |
| using Dbg = System.Management.Automation; | |
| namespace Microsoft.PowerShell.Commands | |
| { | |
| /// <summary> | |
| /// A command that begins a transaction. | |
| /// </summary> | |
| [] | |
| public class StartTransactionCommand : PSCmdlet | |
| { | |
| /// <summary> | |
| /// The time, in minutes, before this transaction is rolled back | |
| /// automatically. | |
| /// </summary> | |
| [] | |
| [] | |
| public int Timeout | |
| { | |
| get | |
| { | |
| return (int)_timeout.TotalMinutes; | |
| } | |
| set | |
| { | |
| // The transactions constructor treats a timeout of | |
| // zero as infinite. So we fudge it to be a bit longer. | |
| if (value == 0) | |
| _timeout = TimeSpan.FromTicks(1); | |
| else | |
| _timeout = TimeSpan.FromMinutes(value); | |
| _timeoutSpecified = true; | |
| } | |
| } | |
| private bool _timeoutSpecified = false; | |
| private TimeSpan _timeout = TimeSpan.MinValue; | |
| /// <summary> | |
| /// Gets or sets the flag to determine if this transaction can | |
| /// be committed or rolled back independently of other transactions. | |
| /// </summary> | |
| [] | |
| public SwitchParameter Independent | |
| { | |
| get { return _independent; } | |
| set { _independent = value; } | |
| } | |
| private SwitchParameter _independent; | |
| /// <summary> | |
| /// Gets or sets the rollback preference for this transaction. | |
| /// </summary> | |
| [] | |
| public RollbackSeverity RollbackPreference | |
| { | |
| get { return _rollbackPreference; } | |
| set { _rollbackPreference = value; } | |
| } | |
| private RollbackSeverity _rollbackPreference = RollbackSeverity.Error; | |
| /// <summary> | |
| /// Creates a new transaction. | |
| /// </summary> | |
| protected override void EndProcessing() | |
| { | |
| if (ShouldProcess( | |
| NavigationResources.TransactionResource, | |
| NavigationResources.CreateAction)) | |
| { | |
| // Set the default timeout | |
| if (!_timeoutSpecified) | |
| { | |
| // See if we're being invoked directly at the | |
| // command line. In that case, set the timeout to infinite. | |
| if (MyInvocation.CommandOrigin == CommandOrigin.Runspace) | |
| { | |
| _timeout = TimeSpan.MaxValue; | |
| } | |
| else | |
| { | |
| _timeout = TimeSpan.FromMinutes(30); | |
| } | |
| } | |
| // Create the new transaction | |
| if (_independent) | |
| { | |
| this.Context.TransactionManager.CreateNew(_rollbackPreference, _timeout); | |
| } | |
| else | |
| { | |
| this.Context.TransactionManager.CreateOrJoin(_rollbackPreference, _timeout); | |
| } | |
| } | |
| } | |
| } | |
| } | |