Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XocvK-0007Rj-52 for pgsql-jdbc@arkaria.postgresql.org; Wed, 12 Nov 2014 18:45:46 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XocvI-0005qh-W0 for pgsql-jdbc@arkaria.postgresql.org; Wed, 12 Nov 2014 18:45:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XocvH-0005qa-UG for pgsql-jdbc@postgresql.org; Wed, 12 Nov 2014 18:45:44 +0000 Received: from mail-pa0-x22e.google.com ([2607:f8b0:400e:c03::22e]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XocvD-0005Xq-6d for pgsql-jdbc@postgresql.org; Wed, 12 Nov 2014 18:45:42 +0000 Received: by mail-pa0-f46.google.com with SMTP id lf10so13437383pab.19 for ; Wed, 12 Nov 2014 10:45:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=MzuXfYjYxwWmQKhg29+1nXyx8GelOjduzGhxBncRdj8=; b=Kc84+hqxQgnanKU6OFRYEw4lcAcXGrGzhuyj9UzvHndVXu93ro4vnWF/wqCBv7BkB6 2RsaU++i7lCGIGIqP0+gF1+X84Y4i86JYF0sVZieULjzGbXP1Rp6EgY78BjisV1y+N1A lnDr2vBh2msTeLY7aRTKK2uWQ2GaqFRi3m11N5jUZFSWZII+wODheDOrPIkAd8OIrHPU vytGU8HKvbjD+7kW28qpfQKyMZ7jqx9Nbx48hODCf+DSeWuaWjksEJowHzMgVHFPfGRJ aGw67WfiIABkPxUVDYeFYTZnsU7LWw8orMHVRC2gZOqmpXPW15UdjsQRHS+6I7HdLG73 CopQ== X-Received: by 10.70.89.15 with SMTP id bk15mr48788385pdb.102.1415817934872; Wed, 12 Nov 2014 10:45:34 -0800 (PST) Received: from [10.10.21.129] (otra.opentable.com. [199.16.144.7]) by mx.google.com with ESMTPSA id ra4sm22944526pab.33.2014.11.12.10.45.34 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 12 Nov 2014 10:45:34 -0800 (PST) Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) Subject: Re: New significance of holdable result sets in Java 8 From: Steven Schlansker In-Reply-To: <771672CB-9567-4966-8CE9-9D84E4F858D8@gmail.com> Date: Wed, 12 Nov 2014 10:45:32 -0800 Cc: "pgsql-jdbc@postgresql.org JDBC" , Brian McCallister Content-Transfer-Encoding: quoted-printable Message-Id: <75C17D7A-95D9-471F-AF64-EFE83D971654@gmail.com> References: <771672CB-9567-4966-8CE9-9D84E4F858D8@gmail.com> To: Marko Topolnik X-Mailer: Apple Mail (2.1878.6) X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org Thanks for bringing this up! I'm not an expert on the PG FE/BE protocol, s= o please excuse my ignorance below :) On Nov 12, 2014, at 7:22 AM, Marko Topolnik wrot= e: >=20 > Data is passed from Controller to View as the return value of a method. T= raditionally, if you wanted a collection-shaped response, you would return = a List. This meant eager loading of all data needed for the response, which= caused scalability issues related to the JVM heap space. We solved this problem (pre-Streams) by inverting the flow of control. In = pseudo-JAX-RS with Jackson: @Path("/objects") public StreamingOutput getObjects() { return os -> { JsonGenerator jg =3D new JsonGenerator(os); ResultSet rs =3D findObjects(); // make sure to do this in a txn with f= etch size set sensibly or you will collect all rows eagerly while (rs.hasNext()) { jg.writeObject(rs.getString("whatever")); } jg.close(); rs.close(); }; } > With the Streams API it is now very convenient to return a lazily-evaluat= ed stream of Model objects. It is also very convenient to make this stream = pull data directly from an underlying ResultSet, tronsforming each row on-t= he-fly into a Model object. This, however, calls for holdable result sets b= ecause the transaction commits when program control leaves the Service laye= r. Having a holdable result set escape the service layer feels like a similar = layering violation as having an open transaction escape the service layer. = Either way a heavyweight database object escapes and must be cleaned up at= a later date. Na=EFvely I would expect the cost of holding the two object= s to be similar, since the actual underlying transaction cannot commit/roll= back until all results are read from the cursor? > The above could raise the level of interest of the PostgreSQL JDBC team i= n implementing holdable result sets backed by native holdable cursors inste= ad of the current client-side cursors, which don't allow the space complexi= ty to be reduced from O(n) to O(1) on the JVM side. I am aware that this is= not a trivial endeavor as it requires intervention into the FE/BE protocol= , but I would nevertheless propose that this concern be reassessed in the l= ight of new developments in the Java ecosystem. As an alternate thought, Stream is AutoCloseable. What if we introduce a S= tatementStream, which takes a (Prepared)Statement, executes in a transactio= n with small fetch size, and then returns a Stream. Again very pseudo= -codey: class DAO { Stream findObjects() { return new StatementResultStream(conn, "SELECT * FROM objects"); // set= s fetch size, ensure auto commit off } } class JsonStreamOutput implements StreamingOutput { StreamOutput(Supplier> streamSupplier) { this.streamSupplier = =3D streamSupplier; } public void write(OutputStream os) { try (JsonGenerator jg =3D new JsonGenerator(os); Stream str =3D streamSupplier.get()) { str.forEach(obj -> jg.writeObject(obj)); } } } @Path("/objects") class Resource { @GET public StreamOutput getObjects() { return new JsonStreamOutput(() -> dto.findObjects().filter(...).map(dto= -> new TransformedDto(dto))); } } The StatementResultStream and StreamOutput classes are entirely reusable. = The DAO is no more complicated than before, except instead of using JDBC di= rectly you punt the work until the Stream undergoes a terminal operation. = The main difficulty here is figuring out a sensible API for StatementResult= Stream construction. The only bit I'm not happy with is the fact that you have to lift your inte= rmediate stream operations into a lambda. You might be able to avoid this = by instead writing the whole bit in terms of a Spliterator instead and usin= g StreamSupport.stream(spliterator), but that introduces questions about he= ld resources since Spliterator is not AutoCloseable. This has the advantage that it's implementable today without changes to eit= her JDBC or PGJDBC, and introduces only reusable optional components to e.g= . Spring or JDBI or whatever library wishes to do this. Thoughts? Steven --=20 Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc