简体   繁体   English

休眠多个事务的优点和缺点

[英]nhibernate multiple transactions advatages and disadvantages

Hi I have a problem with NHibernate transactions. 嗨,我对NHibernate交易有疑问。

I have to make update a couple of times, but when I use: 我必须进行几次更新,但是当我使用时:

using (var trans = session.BeginTransaction())
{
        .....
        trans.commit();
}  

A commit takes me very long time about 1 minute or something (It's not acceptable in my case) 一次提交大约要花我一分钟左右的时间(在我的情况下,这是不可接受的)

So I use multiple transaction per single update and I have something like this: 所以我每次更新都使用多个事务,所以我有这样的事情:

using (var trans1 = session.BeginTransaction())
{
        .....
        trans1.commit();
}
using (var trans1 = session.BeginTransaction())
{
        .....
        trans1.commit();
} 
using (var trans1 = session.BeginTransaction())
{
        .....
        trans1.commit();
} 
...

And a single commit takes about one second or less (this is acceptable time for me) 一次提交大约需要一秒钟或更短的时间(这对我来说是可以接受的时间)

So my question is there are so advantages/disadvantages of this approach? 所以我的问题是这种方法有什么优点/缺点? I could only mention that: Single transaction: 我只能提一下: 单笔交易:

Advantages: 好处:

  1. less time on 'creating' transaction 更少的“创建”交易时间

Disadvantages: 缺点:

  1. very long lock on db, because of long commit 由于长时间提交,对数据库的锁定非常长

Multiple Transaction 多重交易

Advantages 好处

  1. Very short locks on db, because of fast commits 由于快速提交,对数据库的锁定非常短

Disadvantages 缺点

  1. much more time spend on 'creating' transaction 在“创建”交易上花费了更多时间

For me more important is time spending on 'locking db' than creating transactions. 对我来说,更重要的是花时间在“锁定数据库”上而不是创建事务。 So there are any other (dis)advantages of this approaches? 那么这种方法还有其他(缺点)优势吗? Or there are also other approaches which are better? 还是还有其他更好的方法?

I'd like to suggest it's less about time and more about ACID ... and more specifically are those updates a logical 'whole' ? 我想建议您花更少的时间而更多地了解ACID ...更具体地说,这些更新是否合乎逻辑? This should be the key when it comes to transaction granularity. 在事务粒度方面,这应该是关键。

Another way to decide would be what happens if one of the updates fails? 另一种决定方式是,如果其中一个更新失败,将会发生什么? It sounds like you don't want all the updates to rollback, so splitting them is fine (perhaps therefore parallelable and thus would be much quicker?) 听起来您希望所有更新都回滚,所以拆分它们就很好了(也许因此是并行的,因此会更快吗?)

Not knowing the details of your application: Doing multiple transactions for related root aggregates seems fine. 不知道您的应用程序的详细信息:对相关的根聚合执行多个事务似乎很好。

In other words when the updates are on different items you might consider putting those in some repository.Update(newItem). 换句话说,当更新位于不同的项目上时,您可以考虑将其放在某个存储库中。Update(newItem)。

CurrentItem ci = repository.get(id) 
ci.dataUpdate(newInput)
Repository.Update(ci)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM