Paul Gielens:ThoughtsService

another Endpoint to my thoughts

News

Syndication

Ads


Favorites

Projects

Having fun with System.Transactions

Just having fun with System.Transactions in Whidbey. The Bank class implements a couple of interfaces (ITransactionManager, IEnlistmentNotification, IResourceManager) making it possible to enlist the Bank class in the main programs TransactionScope. The Bank class participates in the transactions outcome and rolls back any changes made to its fund. This is by no means a reliable example explaining System.Transactions.

#region Using directives

using System;
using
System.Collections.Generic;
using
System.Text;
using
System.Threading;
using
System.Transactions;

#endregion

 

namespace PaulGielens.Samples.Transactions.TransactedWork
{
    class Program
   
{
        static void
Main(string[] args)
        {
            Bank bank1 = null;
            Bank bank2 = null;

            TransactionScope scope = new TransactionScope();

            try
           
{
                using (scope)
                {
                    bank1 = new Bank(100);
                    bank2 = new Bank(0);
                    bank1.TransferTo(bank2, 25);

                    scope.Consistent = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " , rolling back changes");
            }
            finally
           
{
                // Apparently it takes some time?!?
               
Thread.Sleep(100);
                Console.WriteLine("Bank1 fund = {0} and bank2 fund = {1}",
                    bank1.Fund,
                    bank2.Fund);
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();
           }
        }
    }
}

#region Using directives

using System;
using
System.Collections.Generic;
using
System.Text;
using
System.Transactions;

#endregion

namespace PaulGielens.Samples.Transactions.TransactedWork
{
    public class Bank :
        ITransactionManager,
        IEnlistmentNotification,
        IResourceManager
   
{
        private int fund;
        private int preparedFund;

        #region ITransactionManager Implementation

        public ICommittableTransaction CreateTransaction()
        {
            throw new NotImplementedException();
        }

        public ICommittableTransaction CreateTransaction(TimeSpan span)
        {
            throw new NotImplementedException();
        }

        public ICommittableTransaction CreateTransaction(TransactionOptions options)
        {
            throw new NotImplementedException();
        }

        public IEnlistment ReenlistTransaction(IResourceManager resourceManager,
            byte[] recoveryInformation,
            IEnlistmentNotification enlistmentNotification)
        {
            throw new NotImplementedException();
        }

        public void ResourceManagerRecoveryComplete(IResourceManager resourceManager)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnlistmentNotification Implementation

        public void Commit(IEnlistment enlistment)
        {
            throw new NotImplementedException();
        }

        public void InDoubt()
        {
            throw new NotImplementedException();
        }

        public void Prepare(IPreparingEnlistment preparingEnlistment, byte[] recoveryInformation)
        {
            preparedFund = fund;
        }

        public void Rollback(IEnlistment enlistment)
        {
            fund = preparedFund;
        }

        #endregion       

        #region IResourceManager

        public Guid Identifier
        {
            get { return Guid.NewGuid(); }
        }

        #endregion

        private void Prepare()
        {
            Prepare(null, new byte[] {});
        }

        public Bank(int amount)
        {
            Transaction.Current.DurableEnlist(this, this, false);
            this.fund = amount;
        }

        private void Withdraw(int amount)
        {
            Prepare();
            fund -= amount;
        }

        private void Deposit(int amount)
        {
            Prepare();
            fund += amount;
        }

        public void TransferTo(Bank to, int amount)
        {
            Withdraw(amount);
            throw new Exception("Failed to deposit");
        }

        public int Fund
        {
            get { return fund; }
        }
    }
}

 

Posted: Feb 19 2005, 05:46 PM by p.gielens | with 7 comment(s)
Filed under:

Comments

No Comments