Achieving NHibernate Nested Transaction Behavior

I'm trying to do some kind of nested transaction behavior using NHibernate transaction management and FlushMode options, but things got a bit confusing after reading too much, so any confirmation of the facts below will be very helpful.

I want to open one big transaction, which is split into small transactions. Imagine the following scenario:

  • TX1 opens TX and inserts a Person entry;
  • TX2 opens TX and updates this Person name to P2;
  • TX2 Transmission:
  • TX3 opens TX and updates the name of this person to P3;
  • Rollback TX3;
  • TX1 Transmission:

I would like NH to send INSERT and TX2 UPDATE to the database, just ignoring what TX3 was when it was dropped.

I tried using FlushMode = Never and only cleared the session after the correct Begins / Commits / Rollbacks were required, but NH always updates the database with the final state of the object, regardless of commits and rollbacks. This is normal? Does NH really ignore transactional controls when working with FlushMode = Never?

I also tried using FlushMode = Commit and opening nested transactions, but I found that since ADO.NET, nested transactions are actually the same transaction.

Please note that I am not trying to achieve all-or-nothing behavior. I look more at the save method. Is there a way to do this (savepoints) with NH?

Thanks in advance.

Philip

+4
source share
2 answers

In order not to leave this question open forever, I will publish the decision we made.

We have a unit of work, such as a container, that controls the behavior of nested transactions. Depending on the type of treatment we want, it creates (or not) new sessions. Example:

  • Continue the mistake: if we want even if other commits occurred during the transaction, the UoW container uses different sessions for each "transaction" and resets each tx at the end of its work;
  • Rollback on error: if we want to roll back the session (due to an error or rollback of the business) every other transaction rolls back, the UoW container uses the same session for all nested transactions and rolls back all to the end.

Itโ€™s important to say that the โ€œtransactionโ€ that UoW manipulates is not directly related to the NH transaction (ADO.NET). We created an abstraction of the transaction, so the manipulation code โ€œvotesโ€ if our transaction can be completed or rolled back, but the real action occurs only at the end of everything, based on the chosen error strategy.

We know that this use is not very common and is suitable only for certain scenarios (in our case, this is an integration scenario with batch processing), so I will now send the code. If someone thinks this implementation might help, please send me a message and I will be happy to share this code.

Hi,

Philip

+7
source

NHibernate does not support nested transactions. Each ISession can have no more than one active transaction. I'm not sure what you are trying to do, because your sample script does not make sense to me. Transferring transaction 1 after insertion will have the same effect.

+1
source

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


All Articles