1

I know that nhibernate doesnt support nested transactions.

Let's say that I got something like this:

  1. UserService.BeginTransaction (on current session)
  2. UserService.Save
  3. UserService->FeedService
    1. FeedService.BeginTransaction (on current session)
    2. FeedService.Save
    3. FeedService.Commit (on the returned transaction in #3.1)
  4. UserService->AddressService
    1. AddressService.BeginTransaction (on current session)
    2. AddressService.Save
    3. AddressService.Commit (on the returned transaction in #4.1)
  5. UserService.Commit (on the returned transaction in #1)

What happens when commit is invoked in #3.3, is the transaction commited? I need everything to either succeed or fail.

2 Answers 2

0

As Jamie said, transactions should be managed at a higher level to avoid this situation.

However, if you must keep the begin/commit at the "Service" level for whatever reason, you could wrap everything in a TransactionScope, which you'll Complete() only after everything suceeds.

Sign up to request clarification or add additional context in comments.

2 Comments

Where do you suggest to keep begin/commit if not at service level?
@Kugel it can be an infrastructure layer that provides something like per request or per call transactions, or an orchestrator (like a controller)
0

Yes. The BeginTransaction call in 3.1 won't do anything because there is already an active transaction. If you want all of your operations to participate in the same transaction then don't call Begin/End Transaction in 3.x and 4.x.

My advice is to not use transactions in service or repository classes. I either control the transaction at the UI level or create a class that encapsulates the business process.

1 Comment

It adds an NHibernate dependency which I am fine with. Our UI code uses the ISession directly, or passes it to repository/factory constructors so that multiple repositories can participate in a transaction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.