none
Query regarding Transaction Scope. RRS feed

  • Question

  • Hi,

    I have few queries about transaction execution using TransactionScope.

    Kinldy, go throw below sample code:

    static bool MSGQueue()

            {

                MessageQueue q = null;

                bool isSuccess = false;

                try

                {

                    if (!MessageQueue.Exists(@".\private$\emails"))

                    {

                        MessageQueue.Create(@".\private$\Emails", true);

                    }

                    EmailMessage emm = new EmailMessage();

                    emm.ToAddress = "Peter.Vogel@phvis.com";

                    emm.Subject = "Your email confirmation";

                    emm.Body = "Your purchase has been approved.";

                   

                    q = new MessageQueue(@".\private$\emails");

                    Message msg = new Message();

                    msg.Body = emm;

                    msg.Recoverable = true;

                    msg.Label = "New Message " + DateTime.Now.ToShortTimeString();

                    msg.Formatter = new XmlMessageFormatter(new Type[] { emm.GetType() });

                    q.Send(msg, MessageQueueTransactionType.Automatic);

                    isSuccess = true;

                }

                catch (Exception ex)

                {

                    isSuccess = false;

                }

                finally

                {

                    q.Close();

                }

                return isSuccess;

            }

    static bool SQLServer()

            {

                bool isSucceded = false;

                try

                {

                    // Open a connection to SQL Server 2005

                    using (SqlConnection oCn1 = new SqlConnection("data source=.;initial catalog=Auditing;integrated security=True;"))

                    {

                       

                        SqlCommand oCmd1 = new SqlCommand("UPDATE [Auditing].[dbo].[ EventsMaster] SET [Description] = 'Logged when activity is added. Test' WHERE [AuditEventID] = 2", oCn1);

                        oCn1.Open();

                        // At this point, the connection is in the transaction scope,

                        // which is a lightweight transaction.

                        oCmd1.ExecuteNonQuery();

                        oCn1.Close();

                        isSucceded = true;

                    }

                }

                catch (Exception ex)

                {

                    isSucceded = false;

                }

                return isSucceded;

            }

    static void RunTransaction()

            {

                try

                {

                    // Create the TransactionScope

                    using (TransactionScope oTranScope = new TransactionScope(TransactionScopeOption.Required))

                    {

                        bool isSuccessMSMQ = MSGQueue();

                        bool isSQL = SQLServer();

                        // Tell the transaction scope to commit when ready

                        oTranScope.Complete();

                        // The following bracket completes and disposes the transaction

                    }

                }

                catch (Exception ex)

                {

                }

            }

    Above code queues a message in MSMQ and Updates a field in SQL server within the same TransactionScope.

    If everything goes well (normal circumstances) both the commits (Msg is queued in MSMQ & fields in updated in SQL)

    I would like to know what happens when lets say the SQL Server is unavailable (Service Stopped), will the transaction behave as one unit and both the updates (Msg queued in MSMQ & fields in updated in SQL) will be rolled-back or the message will be queued but since SQL Server is unavailable the field will not be updated?

    If msg is queued when SQL server is unavailable how can I make it behave as one unit? What is the advantage of TransactionScope?


    Sachin R. Chavan.

    Friday, August 22, 2014 8:59 AM

Answers

All replies