Return-Path: X-Original-To: apmail-cayenne-commits-archive@www.apache.org Delivered-To: apmail-cayenne-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A4047DB86 for ; Fri, 24 May 2013 18:32:32 +0000 (UTC) Received: (qmail 37763 invoked by uid 500); 24 May 2013 18:32:32 -0000 Delivered-To: apmail-cayenne-commits-archive@cayenne.apache.org Received: (qmail 37744 invoked by uid 500); 24 May 2013 18:32:32 -0000 Mailing-List: contact commits-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cayenne.apache.org Delivered-To: mailing list commits@cayenne.apache.org Received: (qmail 37735 invoked by uid 99); 24 May 2013 18:32:32 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 24 May 2013 18:32:32 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 24 May 2013 18:32:29 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id C058C2388A4A; Fri, 24 May 2013 18:32:08 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1486156 - /cayenne/main/trunk/docs/docbook/cayenne-guide/src/docbkx/performance-tuning.xml Date: Fri, 24 May 2013 18:32:08 -0000 To: commits@cayenne.apache.org From: aadamchik@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130524183208.C058C2388A4A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: aadamchik Date: Fri May 24 18:32:08 2013 New Revision: 1486156 URL: http://svn.apache.org/r1486156 Log: CAY-1829 Make ResultIterator implement Iterable, create ObjectContext.iterate method docs Modified: cayenne/main/trunk/docs/docbook/cayenne-guide/src/docbkx/performance-tuning.xml Modified: cayenne/main/trunk/docs/docbook/cayenne-guide/src/docbkx/performance-tuning.xml URL: http://svn.apache.org/viewvc/cayenne/main/trunk/docs/docbook/cayenne-guide/src/docbkx/performance-tuning.xml?rev=1486156&r1=1486155&r2=1486156&view=diff ============================================================================== --- cayenne/main/trunk/docs/docbook/cayenne-guide/src/docbkx/performance-tuning.xml (original) +++ cayenne/main/trunk/docs/docbook/cayenne-guide/src/docbkx/performance-tuning.xml Fri May 24 18:32:08 2013 @@ -137,49 +137,44 @@ for(DataRow row : rows) { idea to do so. You can optimize processing of very large result sets with two techniques discussed in this and the following chapter - iterated and paginated queries. Iterated query is not actually a special query. Any selecting query can be executed in - iterated mode by the DataContext (like in the previous example, a cast to DataContext is - needed). DataContext returns an object called ResultIterator that is backed - by an open ResultSet. Data is read from ResultIterator one row at a time until it is - exhausted. Data comes as a DataRows regardless of whether the orginating query was - configured to fetch DataRows or not. A ResultIterator must be explicitly closed to avoid - JDBC resource leak. - Iterated query provides constant memory performance for arbitrarily large ResultSets. - This is true at least on the Cayenne end, as JDBC driver may still decide to bring the - entire ResultSet into the JVM memory. - Here is a full - example:// you need to cast ObjectContext to DataContext to get access to 'performIteratedQuery' -DataContext dataContext = (DataContext) context; + iterated mode by an ObjectContext. ObjectContext creates an object called + ResultIterator that is backed by an open ResultSet. Iterator provides + constant memory performance for arbitrarily large ResultSets. This is true at least on + the Cayenne end, as JDBC driver may still decide to bring the entire ResultSet into the + JVM memory. + Data is read from ResultIterator one row/object at a time until it is exhausted. There + are two styles of accessing ResultIterator - direct access which requires explicit + closing to avoid JDBC resources leak, or a callback that lets Cayenne handle resource + management. In both cases iteration can be performed using "for" loop, as ResultIterator + is "Iterable". + Direct access. Here common sense tells us that ResultIterators instances should be + processed and closed as soon as possible to release the DB connection. E.g. storing open + iterators between HTTP requests for unpredictable length of time would quickly exhaust + the connection + pool.// create a regular query +SelectQuery<Artist> q = new SelectQuery<Artist>(Artist.class); -// create a regular query -SelectQuery q = new SelectQuery(Artist.class); - -// ResultIterator operations all throw checked CayenneException -// moreover 'finally' is required to close it +ResultIterator<Artist> it = context.iterator(q); try { + for(Artist a : it) { + // do something with the object... + ... + } +} +finally { + it.close(); +} + Same thing with a + callback:SelectQuery<Artist> q = new SelectQuery<Artist>(Artist.class); - ResultIterator it = dataContext.performIteratedQuery(q); - - try { - while(it.hasNextRow()) { - // normally we'd read a row, process its data, and throw it away - // this gives us constant memory performance - Map row = (Map) it.nextRow(); - - // do something with the row... +context.iterate(q, new ResultIteratorCallback<Artist>() { + public void iterate(ResultIterator<Artist> it) { + for(Artist a : it) { + // do something with the object... ... } } - finally { - it.close(); - } -} -catch(CayenneException e) { - e.printStackTrace(); -} -Also - common sense tells us that ResultIterators should be processed and closed as soon as - possible to release the DB connection. E.g. storing open iterators between HTTP requests - and for unpredictable length of time would quickly exhaust the connection pool. +});
Paginated Queries