简体   繁体   中英

nhibernate multiple transactions advatages and disadvantages

Hi I have a problem with NHibernate transactions.

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' ? 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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