blob: a314de9079d5e06b0aa7af92d3dbf37527e791d1 [file] [log] [blame]
Title: Turning off Context Synchronization
<P>A big part of commit operation is updating peer DataContexts with the changes committed to the database. The conext synchronization operation has <TT>O(N)</TT> performance, where <TT>N</TT> is the number of peer DataContexts. In a web application <TT>N</TT> is often the number of concurrent sessions, which can be quite big. The throughout of high volume applications can be improved by turning off peer synchronization. One possible way to do that is via a custom <A href="datacontextfactory.html" title="DataContextFactory">DataContextFactory</A>:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">public</SPAN> class IsolatedDataContextFactory <SPAN class="code-keyword">implements</SPAN> DataContextFactory {
<SPAN class="code-keyword">public</SPAN> DataContext createDataContext(DataChannel parent, ObjectStore objectStore) {
<SPAN class="code-comment">// stop listening <SPAN class="code-keyword">for</SPAN> peer events
</SPAN> parent.getEventManager().removeListener(objectStore);
<SPAN class="code-keyword">return</SPAN> <SPAN class="code-keyword">new</SPAN> DataContext(parent, objectStore);
}
}</PRE>
</DIV></DIV>
<P>Of course doing this may result in some stale data in the peer DataContexts, so a decision whether to use this particular technique should be made based on the application specifics. Also a factory can have logic to selectively turn off synchronization for a subset of DataContexts based on some criteria.</P>
<DIV class="panelMacro"><TABLE class="tipMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="http://cayenne.apache.org/docs/3.0/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD><B>Don't use this for idle applications</B><BR>Note that this optimization does not affect percieved user commit time, as synchronization is done in a separate thread. It only affects the overall throughput. So it only makes sense if your application already utilizes close to a 100% of CPU.</TD></TR></TABLE></DIV>
<P><EM>Further improvements in this area are tracked via <A href="http://issues.apache.org/cayenne/browse/CAY-554" class="external-link" rel="nofollow">CAY-554 Jira issue</A>.</EM></P>