DAO, cascade, transaction

44 views
Skip to first unread message

Florian DREVET

unread,
Oct 13, 2011, 4:21:22 PM10/13/11
to nhu...@googlegroups.com
Hi,

I have an issue with NHibernate 2.1 that I'm not able to solve.

Here is the context :
  • I'm building an application with : a presentation layer (ASP.NET), relying on a a business layer (singletons), relying on a datasource layer as singletons, in our case the datasource is NHibernate, throught DAO, with Open-in-View session configured with "auto-commit" (legacy)
  • the cutomer entity is linked to basket items with one-to-many (bag, cascade="all")
  • the cutomer entity is linked to order items with one-to-many (bag, cascade="all")
  • I have to convert a customer's  basket to an order, so I :
    • check products availability
    • begin a transaction (on current NHibernate session)
    • "move" basket's products to order (I remove products from basket, then add them to newly created order)
    • update product's stock
    • commit or rollback (depending of potential errors)
When the convertion fail, the transaction is rolled back, and the data are not uptaded to database (OK)

But when the session is flushed at the end of the HTTP request (legacy...), I have an unexpected "Could not synchronize database state with session state", inner exception is complaining about an insert query on the order item.

As far as I can see, it seems to be an issue with the child entites because when I ask NHibernate to save only the order, then rollback, I don't have any problem.

I hope I've been clear (my English is not perfect), thank you for your help and advices.

Best regards,

Brandon

unread,
Oct 13, 2011, 5:42:01 PM10/13/11
to nhu...@googlegroups.com, nhu...@googlegroups.com
Are you using transactions?  If so, do you have more than one transaction in the session?

Brandon Law
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/4p196oCbVU8J.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.

Florian DREVET

unread,
Nov 7, 2011, 1:08:27 PM11/7/11
to nhu...@googlegroups.com
Hi Blaw,

Thank you for your answer.

I'm using one NHibernate session (since I use session per request pattern)

I'm opening only one transaction at a given time (as far as I know, correct me if I'm wrong, their can be only one transaction opened for a NHibernate session)

brandon law

unread,
Nov 7, 2011, 5:16:18 PM11/7/11
to nhu...@googlegroups.com
There can only be on open AT A TIME.

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/F9brKYhiHm8J.

Florian DREVET

unread,
Nov 8, 2011, 2:49:30 AM11/8/11
to nhu...@googlegroups.com
OK, thank you for the confirmation.

If I :
1) open the transaction
2) delete an object
3) rollback if the deletion failed

Is it normal that the Unit Of Work try to delete the entity at the end of the HTTP request (transaction flushed) ?

Jason Meckley

unread,
Nov 8, 2011, 8:45:52 AM11/8/11
to nhu...@googlegroups.com
when you call session.Delete(entity) the action to delete is queued, but it doesn't actually occur unit the session is flushed. either calling session.Flush() explicitly, which you shouldn't need to ever do. Or calling transaction.Commit(), which then calls session.Flush();

now, where this breaks is when the POID strategy is identity, or some other database enforced PK (ie: not guids or hilo). NH needs to make the call immediately therefore when you call session.Delete(entity) it will execute the action right away.

a common approach to session per view is

begin request: open session
before action: begin transaction
do work
after action:
          if no exception: commit
          if exception: rollback
          dispose of transaction
end request: dispose of session

Florian DREVET

unread,
Nov 8, 2011, 12:17:42 PM11/8/11
to nhu...@googlegroups.com
Jason,

We're effectively make wide use of identity strategy.

Following your example, we do these actions :

begin request: open session
before action: begin transaction
do work
after action:
          if no exception: commit
          if exception: rollback
          dispose of transaction
end request: flush session, dispose of session

We started 4 years ago with NHibernate Best Practices with ASP.NET, 1.2nd Ed. (because we're making a lot of ASP.NET applications), although it's a very interesting guide to make ASP.NET based NHibernate applications, it sounds to me that this architecture was not the best choice to use transactions.

Does I'm wrong ?

Jason Meckley

unread,
Nov 8, 2011, 1:44:20 PM11/8/11
to nhu...@googlegroups.com
remove the explicit call to flush at the end of the request. just dispose of the transaction at the end of the request. other than that, it's textbook session per view.

Florian DREVET

unread,
Nov 8, 2011, 1:49:11 PM11/8/11
to nhu...@googlegroups.com
Jason,

Thank you for your answer, I'm going to make some tests ASAP !
Reply all
Reply to author
Forward
0 new messages