Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I work with NHibernate to 4 years. Previously I worked with the <a href="http://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch02.html%20#%20session-per-operation" rel="nofollow">"open-session-per-operation" antipattern</a>. 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".</p> <p>Recently, I studied the <a href="http://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch02.html-long%20conversations" rel="nofollow">"Conversation pattern"</a> and I did an implementation over the the <a href="http://www.springframework.net/" rel="nofollow">"Spring.Net</a>" infrastructure. The implementation was submitted to <a href="https://jira.springsource.org" rel="nofollow">"jira.springsource"</a> at the <a href="https://jira.springsource.org/browse/SPRNET-1431" rel="nofollow">Issue SPRNET-1431 (Workaround for 'conversation scope' and "session-per-conversation")</a>.</p> <p>I made no "sample application", but if you're interested I can do that.</p> <p>Hailton</p> <h2>Supplementary Answer:</h2> <p>I prepared the sample application and posted in <a href="https://jira.springsource.org/browse/SPRNET-1431" rel="nofollow">SPRNET-1431 Workaround for 'conversation scope' and "session-per-conversation"</a> as the file "Spring.Conversation.example.7z".</p> <p>Below, I wrote explanations to clarify (or not) what I did.</p> <p>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").</p> <p>In this Sample Application the objective is demonstrate:</p> <ol> <li>How to keep instances of objects in a mimicry of "conversation scope". Shown in <code>expression="@(convCustomer)['CustomerEditController']"</code>.</li> <li>How to enjoy the "extended persistence context". "Lazy load errors" do not happen anymore and repeated calls to <code>ISession.Get</code> to 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 line <code>this.Conversation.StartResumeConversation();</code> then you will see the error "failed to lazily initialize a collection of role" occurring at the click of the button "+" on "CustomerList.aspx".</li> </ol> <p>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 (<code>IConversationState.EndConversation()</code>). Recommendation: Keep <code>&lt;property name="EndPaused" value="true"/&gt;</code> in "Spring.Conversation.Imple.WebConversationManager", so when start a conversation the others are discarded.</p> <p>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. </p> <p>List of changes in "Spring.Data.NHibernate.Northwind":</p> <ul> <li>Spring.Northwind.Web.References.2008 <ul> <li>Adding links to schems to enable auto complete.</li> </ul></li> <li><p>web.config</p> <ul> <li><p>module, added: </p> <pre><code>&lt;add name="ConversationModule" type="Spring.Conversation.HttpModule.ConversationModule, Spring.Conversation"/&gt; &lt;add name="ConversationModule" type="Spring.Conversation.HttpModule.ConversationModule, Spring.Conversation"/&gt; </code></pre></li> <li><p>module, removed: </p> <pre><code>&lt;add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate21"/&gt; </code></pre></li> </ul></li> <li><p>web.xml</p> <ul> <li><p>module configurations </p> <pre><code>&lt;!--Configuration for Spring HttpModule interceptor's--&gt; &lt;object name="HttpApplicationConfigurer" type="Spring.Context.Support.HttpApplicationConfigurer, Spring.Web"&gt; &lt;property name="ModuleTemplates"&gt; &lt;dictionary&gt; &lt;entry key="ConversationModule"&gt; &lt;!-- this name must match the module name --&gt; &lt;object&gt; &lt;!-- select "view source" in your browser on any page to see the appended html comment --&gt; &lt;property name="ConversationManagerNameList"&gt; &lt;list element-type="string"&gt; &lt;value&gt;conversationManager&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/object&gt; &lt;/entry&gt; &lt;/dictionary&gt; &lt;/property&gt; &lt;/object&gt; </code></pre></li> <li><p>conversation manager </p> <pre><code>&lt;!--Conversation Manager--&gt; &lt;object name="conversationManager" type="Spring.Conversation.Imple.WebConversationManager, Spring.Conversation" scope="session"&gt; &lt;property name="SessionFactory" ref="NHibernateSessionFactory"/&gt; &lt;property name="EndPaused" value="true"/&gt; &lt;/object&gt; </code></pre></li> <li><p>Customer Conversation </p> <pre><code>&lt;!-- Conversation for 'CustomerEditor.aspx', 'CustomerList.aspx', 'CustomerOrders.aspx', 'CustomerView.aspx', and 'FulfillmentResult.aspx' --&gt; &lt;!-- Important: If the application had other parties ("management employees" for example), they should use another conversation. --&gt; &lt;object name="convCustomer" type="Spring.Conversation.Imple.WebConversationSpringState, Spring.Conversation" scope="session"&gt; &lt;property name="Id" value="convCustomer"&gt;&lt;/property&gt; &lt;property name="TimeOut" value="0"&gt;&lt;/property&gt; &lt;property name="ConversationManager" ref="conversationManager"&gt;&lt;/property&gt; &lt;property name="SessionFactory" ref="NHibernateSessionFactory"/&gt; &lt;property name="DbProvider" ref="DbProvider"/&gt; &lt;!-- Using workaround for 'conversation scope' to reference for 'CustomerEditController'. It is not as volatile as "request scope" not as durable as the "session scope" --&gt; &lt;property name="['CustomerEditController']" ref="CustomerEditController"&gt;&lt;/property&gt; &lt;/object&gt; </code></pre></li> <li><p>Change "CustomerEditController" scope, remove [scope="session"] and put [singleton="false"]: </p> <pre><code>&lt;object name="CustomerEditController" type="NHibernateCustomerEditController" singleton="false"&gt; &lt;constructor-arg name="sessionFactory" ref="NHibernateSessionFactory"/&gt; &lt;/object&gt; ... </code></pre></li> <li><p>Change reference for <code>"CustomerEditController"</code>, remove <code>ref="CustomerEditController"</code> and put <code>expression="@(convCustomer)['CustomerEditController']"</code> (Simulating "conversation scope"): </p> <pre><code>&lt;!-- Using workaround for 'conversation scope' to reference for 'CustomerEditController'. It is not as volatile as "request scope" not as durable as the "session scope" --&gt; &lt;object name="CustomerEditPage" abstract="true"&gt; &lt;property name="CustomerEditController" expression="@(convCustomer)['CustomerEditController']"/&gt; &lt;property name="Conversation" ref="convCustomer"/&gt; &lt;/object&gt; </code></pre></li> </ul></li> </ul> <pre class="lang-xml prettyprint-override"><code> &lt;!-- Using workaround for 'conversation scope' to reference for 'CustomerEditController'. It is not as volatile as "request scope" not as durable as the "session scope" --&gt; &lt;object type="CustomerView.aspx"&gt; &lt;property name="CustomerDao" ref="CustomerDao" /&gt; &lt;property name="CustomerEditController" expression="@(convCustomer)['CustomerEditController']" /&gt; &lt;property name="Conversation" ref="convCustomer"/&gt; &lt;property name="Results"&gt; &lt;dictionary&gt; &lt;entry key="EditCustomer" value="redirect:CustomerEditor.aspx" /&gt; &lt;entry key="CustomerList" value="redirect:CustomerList.aspx" /&gt; &lt;/dictionary&gt; &lt;/property&gt; &lt;/object&gt; </code></pre> <pre class="lang-xml prettyprint-override"><code> &lt;!-- Using workaround for 'conversation scope' to reference for 'CustomerEditController'. It is not as volatile as "request scope" not as durable as the "session scope" --&gt; &lt;object type="FulfillmentResult.aspx"&gt; &lt;property name="FulfillmentService" ref="FulfillmentService" /&gt; &lt;property name="CustomerEditController" expression="@(convCustomer)['CustomerEditController']" /&gt; &lt;property name="Conversation" ref="convCustomer"/&gt; &lt;property name="Results"&gt; &lt;dictionary&gt; &lt;entry key="Back" value="redirect:CustomerOrders.aspx" /&gt; &lt;/dictionary&gt; &lt;/property&gt; &lt;/object&gt; </code></pre> <pre class="lang-xml prettyprint-override"><code> &lt;object type="Default.aspx"&gt; &lt;property name="Conversation" ref="convDefault"/&gt; &lt;property name="Results"&gt; &lt;dictionary&gt; &lt;entry key="CustomerList" value="redirect:CustomerList.aspx" /&gt; &lt;/dictionary&gt; &lt;/property&gt; &lt;/object&gt; </code></pre> <pre class="lang-xml prettyprint-override"><code> &lt;!--Conversation for 'Default.aspx'--&gt; &lt;!-- "convDefault" will have only one functionality: trigger the release of other conversations when started (StartResumeConversation()) --&gt; &lt;object name="convDefault" type="Spring.Conversation.Imple.WebConversationSpringState, Spring.Conversation" scope="session"&gt; &lt;property name="Id" value="convDefault"&gt;&lt;/property&gt; &lt;property name="TimeOut" value="0"&gt;&lt;/property&gt; &lt;property name="ConversationManager" ref="conversationManager"&gt;&lt;/property&gt; &lt;property name="SessionFactory" ref="NHibernateSessionFactory"/&gt; &lt;property name="DbProvider" ref="DbProvider"/&gt; &lt;/object&gt; </code></pre> <ul> <li>Added "ConversationPage.cs". Base page with support for conversation.</li> <li>CustomerList.aspx <ul> <li>Allow list the "Order's" on the same page without "lazy initialization error". All objects stay attached to ISession (NHibernate).</li> </ul></li> <li>CustomerList.aspx.cs: <ul> <li>Added property <code>IList&lt;Customer&gt; CustomersLoadedOncePerConvList</code>. List loaded only once, searching the database only once per conversation.</li> <li>Changing the <code>Page_InitializeControls</code> for consider <code>CustomersLoadedOncePerConvList</code>.</li> <li>The method <code>BtnShowOrders_Click</code> performe implicitly a "lazy load" on <code>Customer.Orders</code>.</li> </ul></li> <li>Change <code>??? : Spring.Web.UI.Page</code> to <code>??? : Spring.Web.UI.Page</code> on: <ul> <li>CustomerEditor.aspx.cs</li> <li>CustomerList.aspx.cs</li> <li>CustomerOrders.aspx.cs</li> <li>CustomerView.aspx.cs</li> <li>FullfillmentResult.aspx.cs</li> <li>Default.aspx.cs </li> </ul></li> <li><p>Dao.xml</p> <ul> <li>Added <code>&lt;entry key="connection.release_mode" value="on_close"/&gt;</code> 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.</li> <li><p>Added: </p> <pre><code>... &lt;entry key="show_sql" value="true"/&gt; &lt;entry key="format_sql" value="true"/&gt; ... </code></pre></li> </ul></li> <li><p>Remove from "Default.aspx.cs" (they are never used):</p> <ul> <li><code>customerDao</code>;</li> <li><code>fulfillmentService</code>;</li> <li><code>CustomerDao</code>;</li> <li><code>Button1_Click(object sender, EventArgs e)</code>;</li> <li><code>ProcessCustomer()</code>;</li> </ul></li> <li><p>Config\Log4Net.xml. </p> <pre><code>... &lt;!--detail's about SQL's. To view sql commands on Logs\log.txt--&gt; &lt;logger name="NHibernate.SQL"&gt; &lt;level value="DEBUG" /&gt; &lt;/logger&gt; ... &lt;!--detail's about Conversation--&gt; &lt;logger name="Spring.Conversation"&gt; &lt;level value="DEBUG" /&gt; &lt;/logger&gt; </code></pre></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload