Received: from smaug.polygnosis.com ([195.139.160.221]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f5QGQLa40614 for ; Tue, 26 Jun 2001 12:26:21 -0400 (EDT) (envelope-from gunnar@smaug.polygnosis.com) Received: (from gunnar@localhost) by smaug.polygnosis.com (8.11.0/8.11.0) id f5QGQG321567; Tue, 26 Jun 2001 18:26:16 +0200 To: Michael Stephenson Cc: "'PostgreSQL jdbc list'" Subject: Re: Re: RE: [ADMIN] High memory usage [PATCH] References: From: Gunnar =?iso-8859-1?q?R=F8nning?= Date: 26 Jun 2001 18:26:16 +0200 In-Reply-To: Message-ID: Lines: 118 User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Archive-Number: 200106/124 X-Sequence-Number: 869 * Michael Stephenson wrote: | | I've just ran a couple of hugely simplified test (basically starting a | couple of thousand threads doing sdf.parse() with either synchronization | or ThreadLocals a couple of times). | I agree that the tests are pretty inconclusive. We need something that simulates real usage better. What platform are you testing on Linux ? If so with native threads or green threads ? | | I personally would advocate applying a patch similar to below to the | current cvs source. | This I don't understand, was your intention to remove thread safety with this patch ? Looking at the way you introduce SimpleDateFormat as an instance variable it seems so. | Michael xxx | | --- PreparedStatement.java.orig Tue Jun 26 16:11:16 2001 | +++ PreparedStatement.java Tue Jun 26 16:16:26 2001 | @@ -44,6 +44,9 @@ | private static ThreadLocal tl_df = new ThreadLocal(); // setDate() SimpleDateFormat | private static ThreadLocal tl_tsdf = new ThreadLocal(); // setTimestamp() SimpleDateFormat | | + private SimpleDateFormat sdSdf; // setDate SimpleDateFormat | + private SimpleDateFormat stSdf; // setTimeStamp SimpleDateFormat | + | /** | * Constructor for the PreparedStatement class. | * Split the SQL statement into segments - separated by the arguments. | @@ -65,13 +68,18 @@ | this.sql = sql; | this.connection = connection; | | - // might just as well create it here, so we don't take the hit later | | - SimpleDateFormat df = new SimpleDateFormat("''yyyy-MM-dd''"); | - tl_df.set(df); | - | - df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | - tl_tsdf.set(df); | + // set up our SimpleDateFormats | + sdSdf = (SimpleDateFormat)tl_df.get(); | + if (sdSdf == null) { | + sdSdf = new SimpleDateFormat("''yyyy-MM-dd''"); | + tl_df.set(sdSdf); | + } | + stSdf = (SimpleDateFormat)tl_tsdf.get(); | + if (stSdf == null) { | + stSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | + tl_tsdf.set(df); | + } | | for (i = 0; i < sql.length(); ++i) | { | @@ -95,17 +103,6 @@ | templateStrings[i] = (String)v.elementAt(i); | } | | - /** | - * New in 7.1 - overides Statement.close() to dispose of a few local objects | - */ | - public void close() throws SQLException | - { | - // free the ThreadLocal caches | - tl_df.set(null); | - tl_tsdf.set(null); | - super.close(); | - } | - | /** | * A Prepared SQL query is executed and its ResultSet is returned | * | @@ -342,9 +339,7 @@ | */ | public void setDate(int parameterIndex, java.sql.Date x) throws SQLException | { | - SimpleDateFormat df = (SimpleDateFormat) tl_df.get(); | - | - set(parameterIndex, df.format(x)); | + set(parameterIndex, sdSdf.format(x)); | | // The above is how the date should be handled. | // | @@ -381,13 +376,13 @@ | */ | public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException | { | - SimpleDateFormat df = (SimpleDateFormat) tl_tsdf.get(); | - df.setTimeZone(TimeZone.getTimeZone("GMT")); | + stSdf.setTimeZone(TimeZone.getTimeZone("GMT")); | | // Use the shared StringBuffer | synchronized(sbuf) { | sbuf.setLength(0); | - sbuf.append("'").append(df.format(x)).append('.').append(x.getNanos()/10000000).append("+00'"); | + sbuf.append("'").append(stSdf.format(x)).append('.') | + .append(x.getNanos()/10000000).append("+00'"); | set(parameterIndex, sbuf.toString()); | } | | | | | ---------------------------(end of broadcast)--------------------------- | TIP 2: you can get off all lists at once with the unregister command | (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) | -- Gunnar Rønning - gunnar@polygnosis.com Senior Consultant, Polygnosis AS, http://www.polygnosis.com/