question on HelperClass.Transaction

Posts   
 
    
nmarun
User
Posts: 27
Joined: 31-Jul-2006
# Posted on: 30-Jul-2007 20:07:30   

Hi,

Here are the two methods I'm having in my code:


        protected void SaveNotes()
        {
            Transaction TransactionManager = new Transaction(IsolationLevel.ReadCommitted, "SaveNotes");
            try
            {
                ApplicantNoteEntity Note = new ApplicantNoteEntity();
                Note.ApplicantId = ApplicantID;
                Note.Note = NotesTextBox.Text;
                Note.NoteAddedDate = DateTime.Now;
                Note.NoteAddedAuthor = "amahendrakar";
                TransactionManager.Add(Note);
                Note.Save();
                TransactionManager.Commit();
                LoadNotes();
                CurrentNotesPanel.Visible = true;
                
            }
            catch(Exception ex)
            {
                JobApplicationAdmin.Site.Log.Error(ex);
                MessageLabel.Text = "An error has occurred. Please inform the Admin.";
                MessageLabel.Visible = true;
                TransactionManager.Rollback();
                MessageLabel.CssClass = "errorMessage";
            }
            finally
            {
                TransactionManager.Dispose();
            }
        }

        private void LoadNotes()
        {
            IPredicateExpression Filter = new PredicateExpression(ApplicantNoteFields.ApplicantId == ApplicantID);
            ApplicantNoteCollection Note = new ApplicantNoteCollection();
            Note.GetMulti(Filter);
            if (Note.Count > 0)
            {
                StringBuilder Notes = new StringBuilder();
                for (int i = 0; i < Note.Count; i++)
                {
                    Notes.AppendFormat("Author: {0}{1}", Note[i].NoteAddedAuthor, Environment.NewLine);
                    Notes.AppendFormat("Date: {0}{1}", Note[i].NoteAddedDate.ToString("MM/dd/yyyy"), Environment.NewLine);
                    Notes.AppendFormat("Note: {0}{1}", Note[i].Note, Environment.NewLine);
                    Notes.AppendFormat("-------------------------------{0}", Environment.NewLine);
                }
                CurrentNotesTextBox.Text = Notes.ToString();
            }
            else
            {
                CurrentNotesTextBox.Text = "No notes added";
            }
        }

All I'm trying to do is, if there is an error while saving the Note entity, I need to rollback the transaction, if not the transaction gets committed. My question is what happens if the transaction gets committed and an exception occurs in the LoadNotes method? It goes to the catch block of the SaveNotes method and what happens when it tries to do a rollback?

How else could this be coded?

Thanks in advance, Arun

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-Jul-2007 05:46:12   

My question is what happens if the transaction gets committed and an exception occurs in the LoadNotes method? It goes to the catch block of the SaveNotes method and what happens when it tries to do a rollback?

The transaction was committed, so if you need to rollback after a commit, you need to code a delete snippet that delete the already added entities.

       protected void SaveNotes()
        {
            Transaction TransactionManager = new Transaction(IsolationLevel.ReadCommitted, "SaveNotes");
            try
            {
                ApplicantNoteEntity Note = new ApplicantNoteEntity();
                Note.ApplicantId = ApplicantID;
                Note.Note = NotesTextBox.Text;
                Note.NoteAddedDate = DateTime.Now;
                Note.NoteAddedAuthor = "amahendrakar";
                TransactionManager.Add(Note);
                Note.Save();
                TransactionManager.Commit();                
            }
            catch(Exception ex)
            {
                JobApplicationAdmin.Site.Log.Error(ex);
                MessageLabel.Text = "An error has occurred. Please inform the Admin.";
                MessageLabel.Visible = true;
                TransactionManager.Rollback();
                MessageLabel.CssClass = "errorMessage";
            }
            finally
            {
                TransactionManager.Dispose();
            }


            try
            {
                LoadNotes();
            }
            catch(Exception ex)
            {
                    // delete entities already added
            }
             CurrentNotesPanel.Visible = true;

        }

However I don't see an obvious possible exception at your LoadNotes() method.

David Elizondo | LLBLGen Support Team
nmarun
User
Posts: 27
Joined: 31-Jul-2006
# Posted on: 31-Jul-2007 17:19:44   

Thanks daelmo. You answered what I needed to know. Please close this thread.

Arun