Do you know how to implement transactions in Castle ActiveRecord?

0 votes
asked Sep 2, 2008 by nelson-miranda

I decided to make a system for a client using Castle ActiveRecord, everything went well until I found that the transactions do not work, for instance;

               TransactionScope t = new TransactionScope();

               try
               {
                   member.Save();

                   //This is just to see transaction working
                   throw new Exception("Exception");  

                   foreach (qfh.Beneficiary b1 in l)
                   {
                       b1.Create();
                   }


               }
               catch (Exception ex)
               {

                   t.VoteRollBack();
                   MessageBox.Show(ex.Message);
               }
               finally
               {
                   t.Dispose();
               }

But it doesn't work, I throw an Exception just to try the transaction rolls back, but for my surprise I see that the first [Save] records into the database. What is happening?

I'm new on Castle and NHibernate, firstly I saw it very attractive and I decided to go on with it and MySQL (I've never worked with this DB), I tried ActiveWriter and it seemed very promising but after a long and effortly week I see this issue and now I feel like I'm stuck and like I've wasted my time. It is supposed to be easy but right now I'm feeling a frustated cause I cannot find enough information to make this workout, can you help me?

3 Answers

0 votes
answered Sep 2, 2008 by michael-haren

Ben's got it. That doc is a little confusing. Refer to the last block on the page, "Nested transactions".

0 votes
answered Sep 2, 2008 by ben-scheirman

You need to wrap the code in a session scope, like this:

using(new SessionScope())
{
   a.Save();
   b.Save();
   c.Save();
}

Read more here.

0 votes
answered Sep 2, 2008 by nelson-miranda

I finally fixed, it happened that I was doing wrong, I overrode the Save method of the Member class and made sessionScope inside and inside of it a transaction scope, so when a involved all of that in a transaction scope it saved in the database, so when I threw the exception everything was already saved, I think that's it.

All in all, thanks for the help.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...