I have a relatively simple problem, which I cannot tackle without a lot of ugly workarounds. Basically, I am developing a payment gateway client, whereby the process is that the website informs the payment server of the payment to be done, client is transferred to payment server, and once payment is done, payment server informs the website on a seperate thread that the payment was done, and redirects the client. Now, the client can be ‘received’ on the website before the notification of the payment was received. Thus, I need to program the system to wait until the notification is received or 30 seconds at most.
Once the notification is received, an Order entity is marked as ‘Paid’. The problem is this:
- Client is redirected to website before,
Order.Paidis stillfalse - After 1 – 5 seconds, notification is received and
Orderis marked astrue - Due to Nhibernate session management, Client still sees the
Order.Paidasfalseon the other thread. - To make matters worse, sometimes the client’s
Orderis then flushed onEnd_Request, and theOrder.Paidis again reset tofalse.
The only way I tried that seems to work is that I dispose of the Nhibernate session explicitly before waiting, and then constantly create a new session until it is found as paid. However the problem is since the original session was removed, lazy loading for any entities already loaded before hand will stop working and generate errors.
It looks simple in my mind but yet seems to be quite complicated due to session management, any ideas how to go about it?
Thanks!
From the time I posted the question, I’ve managed to find a solution to this.
Basically, how NHibernate works – If any exception occurs on a session, the session must be disposed. This also applies to
StaleObjectException, when using optimistic concurrency. The reason the client was still seeing is as ‘not paid’ was because the session was not being disposed.A book snippet on Google Books explains this in a bit more detail:
http://books.google.com.mt/books?id=eMOYUycDaoUC&pg=PT213&lpg=PT213&dq=nhibernate+staleobjectexception+dispose&source=bl&ots=rg9YHt58gc&sig=W9FoTm8_4DT3PvMKGLrddG3lSxs&hl=en&sa=X&ei=FGp-UNr4FI7Vsga2nIDwBw&ved=0CGAQ6AEwCA