Do you know how to implement transactions in Castle ActiveRecord?

I decided to create a system for the client using Castle ActiveRecord , everything went fine until I found that the transactions did not work, for the 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 this will not work, I throw an exception to try rollback transactions, but, to my surprise, I see that the first [Save] records are written to the database. What's happening?

I am new to Castle and NHibernate, firstly, I saw it very attractive, and I decided to continue with it and MySQL (I never worked with this database), I tried ActiveWriter, and it seemed very promising, but after a long and effortly weeks I see this problem, and now I feel like I'm stuck and how I wasted my time. This is supposed to be easy, but right now I feel a tired reason, I canโ€™t find enough information for this workout, can you help me?

+4
source share
3 answers

Ben got it. This document is a bit confusing. Refer to the last block on the Nested Transactions page .

+3
source

You need to wrap the code in the session area, for example:

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

More details here .

+5
source

Finally, I decided that I was wrong, I redefined the Save method of the Member class and did a sessionScope inside and inside the transaction scope, so when all this is involved in the transaction scope that it saved to the database, so when I selected the exception, everything has already been saved, I think it is.

In general, thanks for the help.

0
source

Source: https://habr.com/ru/post/1276510/


All Articles