Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you know how to implement transactions in Castle ActiveRecord?

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?

like image 442
Nelson Miranda Avatar asked Mar 01 '23 08:03

Nelson Miranda


1 Answers

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

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

Read more here.

like image 68
Ben Scheirman Avatar answered May 03 '23 14:05

Ben Scheirman