开发者

NHibernate & session-per-conversation in AJAX scenario: manual flush needed?

we are facing a very strange problem with our 开发者_运维知识库application, until now i see no option to solve the problem:

The application uses NHibernate (3.1.x) in an session-per-conversation architecture, meaning at start/end of each ASP/IIS request a NHibernate session with a new transaction is opened/closed + flushed/committed.

Now, we have a mailbox in the application including a simple mailgrid: The mailgrid is populated first time when a user enters the mailbox (start-Request creates a new NHibernate session/transaction, loads stuff from DB, on end-Request the session is closed)

The problem occurs at the deletion of a mail:

(1) the deletion of mails does not happen via a full page-reload/refresh, instead it calls a webservice which returns the new rendered HTML grid after a message is deleted. This webservice deletes a mail by doing a simple

T_Mails m = T_Mails.GetMailByID( 12345678 );
m.Status = MailStatusDeleted;
NHibernateSession.Update( m );

(2) After this call to update the mail, the same function also re-renders the updated mailgrid - it does this by querying for the last 5 rows in table T_Mails (one mailbox page contains only 5 rows!); then, this is the current content of the mailbox for a given user.

The problem now is: Since both of these calls occur in the same function of the webservice (and thus in the same session/transaction scope), the mail we want to delete (in step 1), is not deleted since the updated status from NHibernateSession.Update( m ) does not reach the database.

If i open the table via MSSQL ManagementStudio while a debugging session i can see that the status of the mail is not persited to database.

My simple solution for this problem would be: Doing a manual NHibernateSession.Flush() call between updating the mail and reloading the content from the table? But: this would break the session-per-conversation pattern, since NhibernateSession.Flush() is/should occuring at the end of each request - it's not ought to be called manually somewhere in the running request.

Otherwise i'm really stuck.


You should make 2 ajax calls - first one to delete an item and second one to refresh list when first query will be finished. Please make sure that you are making calls in same order. Below is sample code using jQuery

$.ajax({
    url: 'Mail/Delete',
    data: { mailId: someId },
    success: function (data) {
        $.ajax({
            url: 'Mail/Refresh',
            success: function (data) {
                //here you should put new list into list container
            }
        });
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜