你知道如何在Castle ActiveRecord中实现事务吗?

发布于 2024-07-04 05:20:40 字数 1131 浏览 4 评论 0原文

我决定使用 Castle ActiveRecord,一切都很顺利,直到我发现事务不起作用,例如;

               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();
               }

但它不起作用,我抛出一个异常只是为了尝试事务回滚,但令我惊讶的是我看到第一个[保存]记录到数据库中。 怎么了?

我是 Castle 和 NHibernate 的新手,首先我看到它非常有吸引力,我决定继续使用它和 MySQL(我从未使用过这个数据库),我尝试了 ActiveWriter,它看起来很有前途,但经过长时间的努力一周我看到这个问题,现在我觉得我被困住了,就像我浪费了时间。 这应该很容易,但现在我感到很沮丧,因为我找不到足够的信息来进行这项锻炼,你能帮助我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛起拖把扫天下 2024-07-11 05:20:40

您需要将代码包装在会话范围内,如下所示:

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

阅读更多 此处

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

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

Read more here.

清醇 2024-07-11 05:20:40

我终于修复了,碰巧我做错了,我覆盖了Member类的Save方法,并将sessionScope内部和内部都设置为事务范围,所以当a涉及到它保存在数据库中的事务范围中的所有内容时,所以当我抛出异常时,一切都已经保存了,我想就是这样。

总而言之,感谢您的帮助。

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.

七分※倦醒 2024-07-11 05:20:40

本明白了。 该文档有点令人困惑。 请参阅最后一个块 页面上的“嵌套事务”。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文