Hello can some pros with nhibernate give me example implementation of Nhibernate session per conversation or unhaddins implementation?
Or explain how to do that.
Best regards
Endiss
Hello can some pros with nhibernate give me example implementation of Nhibernate session per
Share
I work with NHibernate to 4 years. Previously I worked with the “open-session-per-operation” antipattern. The objects were always detached. So, to persist, I had to reattach them or copy their values to attached objects. This causes many lines of code and many “lazy initialization exeption”.
Recently, I studied the “Conversation pattern” and I did an implementation over the the “Spring.Net” infrastructure. The implementation was submitted to “jira.springsource” at the Issue SPRNET-1431 (Workaround for ‘conversation scope’ and “session-per-conversation”).
I made no “sample application”, but if you’re interested I can do that.
Hailton
Supplementary Answer:
I prepared the sample application and posted in SPRNET-1431 Workaround for ‘conversation scope’ and “session-per-conversation” as the file “Spring.Conversation.example.7z”.
Below, I wrote explanations to clarify (or not) what I did.
This “sample application” is a modification of “Spring.Data.NHibernate.Northwind” contained in the version “1.3.0” of “Spring.NET” to use Conversation.
Currently, the “Spring.Net” has no “conversation scope” nor implements the concept of “Extended Persistence Context” (“session per conversation strategy”).
In this Sample Application the objective is demonstrate:
expression="@(convCustomer)['CustomerEditController']".ISession.Getto a “same record” do not cause numerous visits to the database, more efficient use of the NHibernate cache. The modifications on “CustomerList.aspx” demonstrate this. To verify the effectiveness of Conversation, comment on “App_Code\ConversationPage.cs” the linethis.Conversation.StartResumeConversation();then you will see the error “failed to lazily initialize a collection of role” occurring at the click of the button “+” on “CustomerList.aspx”.IMPORTANT: Never use a single conversation for the entire application (with the same duration of the “HTTP Session”). Remember, NHibernate keeps a cache of all loaded objects, if the conversation is held for a long time this cache tends to grow indefinitely (the limit is the amount of database records). That is, each conversation should be limited to a subset of the application pages, and must be discarded at the end of interaction with this subset (
IConversationState.EndConversation()). Recommendation: Keep<property name="EndPaused" value="true"/>in “Spring.Conversation.Imple.WebConversationManager”, so when start a conversation the others are discarded.ADDITIONAL INFORMATION: The unit tests (“Spring.Northwind.IntegrationTests.2008”) are not working. But there is no problem because it is not related to the changes made to support conversation, in fact they were already resulting in errors even before that.
List of changes in “Spring.Data.NHibernate.Northwind”:
web.config
module, added:
module, removed:
web.xml
module configurations
conversation manager
Customer Conversation
Change “CustomerEditController” scope, remove [scope=”session”] and put [singleton=”false”]:
Change reference for
"CustomerEditController", removeref="CustomerEditController"and putexpression="@(convCustomer)['CustomerEditController']"(Simulating “conversation scope”):IList<Customer> CustomersLoadedOncePerConvList. List loaded only once, searching the database only once per conversation.Page_InitializeControlsfor considerCustomersLoadedOncePerConvList.BtnShowOrders_Clickperforme implicitly a “lazy load” onCustomer.Orders.??? : Spring.Web.UI.Pageto??? : Spring.Web.UI.Pageon:Dao.xml
<entry key="connection.release_mode" value="on_close"/>to avoid disconnection and reconnection before and after each IDbCommand execution. This is important because can occur a large numbers of lazyloads outside the transaction boundaries.Added:
Remove from “Default.aspx.cs” (they are never used):
customerDao;fulfillmentService;CustomerDao;Button1_Click(object sender, EventArgs e);ProcessCustomer();Config\Log4Net.xml.