X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 707909DC84E for ; Thu, 9 Feb 2006 14:24:42 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 04550-06 for ; Thu, 9 Feb 2006 14:24:41 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from jcour.kyon.de (www.ky-on.de [213.9.98.91]) by postgresql.org (Postfix) with ESMTP id 061B79DC850 for ; Thu, 9 Feb 2006 14:24:38 -0400 (AST) Received: from [10.0.4.2] (gw-cust1497.hkn.net [213.9.96.234]) (AUTH: LOGIN tt, ) by jcour.kyon.de with esmtp; Thu, 09 Feb 2006 19:24:38 +0100 Message-ID: <43EB8919.7000509@kyon.de> Date: Thu, 09 Feb 2006 19:25:29 +0100 From: till toenges User-Agent: Thunderbird 1.5 (Macintosh/20051201) Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=_jcour.kyon.de-42591-1139509478-0001-2" To: Kris Jurka CC: pgsql-jdbc@postgresql.org Subject: Re: Bug in TypeInfoCache causes getObject to fail References: <43EB1EA4.4000506@kyon.de> In-Reply-To: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.12 required=5 tests=[AWL=0.120] X-Spam-Score: 0.12 X-Spam-Level: X-Archive-Number: 200602/72 X-Sequence-Number: 14669 This is a MIME-formatted message. If you see this text it means that your E-mail software does not support MIME-formatted messages. --=_jcour.kyon.de-42591-1139509478-0001-2 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 7bit Kris Jurka wrote: > Nice work. Applied to 8.1 and HEAD. Thank you! > Actually it is necessary for the maps to be synchronized because at any > time a client may call PGConnection.addDataType which will modify the maps. Ah, i forgot that the driver needs to be thread safe. I looked at it again, and made a few changes to improve this in other fringe cases. Mostly, i checked where and how the maps were used. I also noticed that there are two PreparedStatements, which might be not thread safe (don't know enough about the internals). The only map that can be accessed from outside the class is _pgNameToSQLType, via an iterator returned from getPGTypeNamesWithSQLType(). That must obviously be thread safe, more on that below. The other maps are not used outside the class, and can be protected by synchronizing the accessing methods. Also, the other methods often rely on a consistent relation between two maps. A simple synchronizedMap ensures not that both maps are in a consistent state relative to each other. Therefore any method accessing any of these maps should be synchronized as a whole. You wrote in the changelog that _pgNameToSQLType is actually static. That can be done in a static initializer, and the member can be static (and final) again. To protect it against any changes, and therefore make it thread safe, an unmodifiableMap seems enough. The methods using the PreparedStatements must be synchronized, because the statements are only created when neccessary, so they can not be used to synchronize on. They also fall under the category of methods that use more than one map at once, see above. I made another patch to the current branch, which i attached. Hope some of this makes sense ;-) Till --=_jcour.kyon.de-42591-1139509478-0001-2 Content-Type: text/plain; x-mac-type=0; x-mac-creator=0; name="TypeInfoCache_HEAD.java.diff"; charset=iso-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="TypeInfoCache_HEAD.java.diff" Index: TypeInfoCache.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/TypeInfoCache.java,v retrieving revision 1.4 diff -u -r1.4 TypeInfoCache.java --- TypeInfoCache.java 9 Feb 2006 16:29:06 -0000 1.4 +++ TypeInfoCache.java 9 Feb 2006 17:58:30 -0000 @@ -30,7 +30,7 @@ public class TypeInfoCache { // pgname (String) -> java.sql.Types (Integer) - private Map _pgNameToSQLType; + private static final Map _pgNameToSQLType; // pgname (String) -> java class name (String) // ie "text" -> "java.lang.String" @@ -48,7 +48,7 @@ private PreparedStatement _getOidStatement; private PreparedStatement _getNameStatement; - private static Object types[][] = { + private static final Object types[][] = { {"int2", new Integer(Oid.INT2), new Integer(Types.SMALLINT), "java.lang.Integer"}, {"int4", new Integer(Oid.INT4), new Integer(Types.INTEGER), "java.lang.Integer"}, {"oid", new Integer(Oid.OID), new Integer(Types.INTEGER), "java.lang.Integer"}, @@ -71,28 +71,38 @@ {"timestamptz", new Integer(Oid.TIMESTAMPTZ), new Integer(Types.TIMESTAMP), "java.sql.Timestamp"} }; + static + { + Map pgNameToSQLType = new HashMap(); + for (int i=0; i