Skip to content

AutoTx Facility The Transaction Attribute

haf edited this page Apr 16, 2011 · 2 revisions

The [Transaction] attribute allows you to declaratively specify what methods should be transactional.

Example usage:

[Transaction]
public virtual Thing LoadNewThing()
{
    // be aware how I'm not manually disposing the ISession here; I could, but it would make no difference
    return _GetSession().Get<Thing>(_Id);
}

The TransactionAttribute implement ITransactionOptions behind the scenes, which is this interface:

[ContractClass(typeof(ITransactionOptionsContract))]
public interface ITransactionOptions : IEquatable<ITransactionOptions>
{
    /// <summary>
    ///     Gets the transaction isolation level.
    /// </summary>
    IsolationLevel IsolationLevel { [Pure] get; }

    /// <summary>
    ///     Gets the transaction mode.
    /// </summary>
    TransactionScopeOption Mode { [Pure] get; }

    /// <summary>
    ///     Gets whether the transaction is read only.
    /// </summary>
    bool ReadOnly { [Pure] get; }

    /// <summary>
    /// Gets whether the current transaction's method should forked off.
    /// </summary>
    bool Fork { [Pure] get; }

    /// <summary>
    ///     Gets the Timeout for this managed transaction. Beware that the timeout 
    ///     for the transaction option is not the same as your database has specified.
    ///     Often it's a good idea to let your database handle the transactions
    ///     timing out and leaving this option to its max value. Your mileage may vary though.
    /// </summary>
    TimeSpan Timeout { [Pure] get; }

    /// <summary>
    /// Gets or sets whether the <see cref="Fork"/> operation should wait for all to complete, or
    /// simply return and let the topmost transaction wait instead.
    /// </summary>
    bool WaitAll { [Pure] get; }

    /// <summary>
    /// Gets or sets whether the commit should be done asynchronously. Default is false. If you have done a lot of work
    /// in the transaction, an asynchronous commit might be preferrable.
    /// </summary>
    bool AsyncCommit { [Pure] get; }
}

XML-documentation in Visual Studio should help you select your preferred options.

Something went wrong with that request. Please try again.