pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Langton <djlangton+pgjdbc@googlemail.com>
To: pgsql-jdbc@postgresql.org
Subject: Connection Properties for FetchSize and Autocommit
Date: Thu, 8 Oct 2009 21:02:27 +0100
Message-ID: <7c49e7040910081302v284f417bk6301b39e3eee474b@mail.gmail.com> (raw)

Hi pgjdbc coders,

I recently had to deal with a 3rd party application (without source code)
that processed large result sets, and kept running out of memory on the
client.  I realise this was because the application didn't set a fetch size,
nor turn off autocommit and so the criteria for the driver to use a
cursor-based result set were not met.

Since I cannot modify the application, I instead altered the postgresql jdbc
driver instead to allow two additional driver parameters - defaultFetchSize
and defaultAutoCommit to override the built-in defaults (0 and true).

Anyway, it worked well for me, so for what its worth a diff is attached.
All test cases still pass. If you think this is worth pursuing, let me know
what else needs to be done!

David Langton

Attachments:

diff.txt (text/plain, 6.0K) download ? build.local.properties Index: org/postgresql/Driver.java.in =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/Driver.java.in,v retrieving revision 1.78 diff -u -r1.78 Driver.java.in --- org/postgresql/Driver.java.in 2 Jun 2009 00:22:58 -0000 1.78 +++ org/postgresql/Driver.java.in 8 Oct 2009 19:55:18 -0000 @@ -459,7 +459,11 @@ { "kerberosServerName", Boolean.FALSE, "The Kerberos service name to use when authenticating with GSSAPI. This is equivalent to libpq's PGKRBSRVNAME environment variable." }, { "jaasApplicationName", Boolean.FALSE, - "Specifies the name of the JAAS system or application login configuration." } + "Specifies the name of the JAAS system or application login configuration." }, + { "defaultFetchSize", Boolean.FALSE, + "Specifies an integer fetchSize to use as a default on all new Statement instances. A better approach is to modify the calling application to set an appropriate size - this parameter is for situations where this is not possible." }, + { "defaultAutoCommit", Boolean.FALSE, + "Sets the autocommit state for all new Connections." } }; /** Index: org/postgresql/jdbc2/AbstractJdbc2Connection.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Connection.java,v retrieving revision 1.52 diff -u -r1.52 AbstractJdbc2Connection.java --- org/postgresql/jdbc2/AbstractJdbc2Connection.java 1 Jul 2009 05:00:40 -0000 1.52 +++ org/postgresql/jdbc2/AbstractJdbc2Connection.java 8 Oct 2009 19:55:19 -0000 @@ -65,6 +65,7 @@ protected int prepareThreshold; // Connection's autocommit state. public boolean autoCommit = true; + public boolean defaultAutoCommit = true; // Connection's readonly state. public boolean readOnly = false; @@ -76,6 +77,9 @@ public abstract DatabaseMetaData getMetaData() throws SQLException; + //default fetch size for new statementinstances + public int defaultFetchSize = 0; + // // Ctor. // @@ -117,6 +121,31 @@ { } + //setup a default fetch size + try + { + this.defaultFetchSize = Integer.parseInt( info.getProperty( "defaultFetchSize", "0" ) ); + } + catch (NumberFormatException nfe) + { + logger.debug("defaultFetchSize property was not an integer"); + } + + //override default autocommit state + try + { + this.defaultAutoCommit = Boolean.parseBoolean( info.getProperty( "defaultAutoCommit", "true" ) ); + } + catch (Exception ace) + { + logger.debug("defaultAutoCommit property could not be parsed as a boolean"); + } + + if( this.defaultAutoCommit != this.autoCommit ) + { + setAutoCommit( this.defaultAutoCommit ); + } + //Print out the driver version number if (logger.logInfo()) logger.info(Driver.getVersion()); Index: org/postgresql/jdbc3/Jdbc3Connection.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc3/Jdbc3Connection.java,v retrieving revision 1.12 diff -u -r1.12 Jdbc3Connection.java --- org/postgresql/jdbc3/Jdbc3Connection.java 8 Jan 2008 06:56:29 -0000 1.12 +++ org/postgresql/jdbc3/Jdbc3Connection.java 8 Oct 2009 19:55:20 -0000 @@ -28,6 +28,7 @@ { Jdbc3Statement s = new Jdbc3Statement(this, resultSetType, resultSetConcurrency, resultSetHoldability); s.setPrepareThreshold(getPrepareThreshold()); + s.setFetchSize( this.defaultFetchSize ); return s; } @@ -36,6 +37,7 @@ { Jdbc3PreparedStatement s = new Jdbc3PreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability); s.setPrepareThreshold(getPrepareThreshold()); + s.setFetchSize( this.defaultFetchSize ); return s; } Index: org/postgresql/jdbc3g/Jdbc3gConnection.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc3g/Jdbc3gConnection.java,v retrieving revision 1.6 diff -u -r1.6 Jdbc3gConnection.java --- org/postgresql/jdbc3g/Jdbc3gConnection.java 8 Oct 2008 18:24:05 -0000 1.6 +++ org/postgresql/jdbc3g/Jdbc3gConnection.java 8 Oct 2009 19:55:20 -0000 @@ -28,6 +28,7 @@ { Jdbc3gStatement s = new Jdbc3gStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability); s.setPrepareThreshold(getPrepareThreshold()); + s.setFetchSize( this.defaultFetchSize ); return s; } @@ -36,6 +37,7 @@ { Jdbc3gPreparedStatement s = new Jdbc3gPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability); s.setPrepareThreshold(getPrepareThreshold()); + s.setFetchSize( this.defaultFetchSize ); return s; } Index: org/postgresql/jdbc4/Jdbc4Connection.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc4/Jdbc4Connection.java,v retrieving revision 1.2 diff -u -r1.2 Jdbc4Connection.java --- org/postgresql/jdbc4/Jdbc4Connection.java 8 Jan 2008 06:56:30 -0000 1.2 +++ org/postgresql/jdbc4/Jdbc4Connection.java 8 Oct 2009 19:55:20 -0000 @@ -28,6 +28,8 @@ { Jdbc4Statement s = new Jdbc4Statement(this, resultSetType, resultSetConcurrency, resultSetHoldability); s.setPrepareThreshold(getPrepareThreshold()); + s.setFetchSize( this.defaultFetchSize ); + return s; } @@ -36,6 +38,7 @@ { Jdbc4PreparedStatement s = new Jdbc4PreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability); s.setPrepareThreshold(getPrepareThreshold()); + s.setFetchSize( this.defaultFetchSize ); return s; }

view thread (5+ messages)  latest in thread

Message-ID: <7c49e7040910081302v284f417bk6301b39e3eee474b@mail.gmail.com>
Permalink:  ../7c49e7040910081302v284f417bk6301b39e3eee474b@mail.gmail.com/
Also on:    postgresql.org/message-id/7c49e7040910081302v284f417bk6301b39e3eee474b@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: djlangton+pgjdbc@googlemail.com
  Subject: Re: Connection Properties for FetchSize and Autocommit
  In-Reply-To: <7c49e7040910081302v284f417bk6301b39e3eee474b@mail.gmail.com>
  References: <7c49e7040910081302v284f417bk6301b39e3eee474b@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox