pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedPreparedStatement clearParameters and setTimestamp
3+ messages / 2 participants
[nested] [flat]
* PreparedStatement clearParameters and setTimestamp
@ 2006-07-25 01:42 Akira Sato <sato.akira@oss.ntt.co.jp>
0 siblings, 1 reply; 3+ messages in thread
From: Akira Sato @ 2006-07-25 01:42 UTC (permalink / raw)
To: pgsql-jdbc
Hi,
This reply is about the following threads.
http://archives.postgresql.org/pgsql-jdbc/2006-02/msg00156.php
With case of setTimestamp(), it seem to always do "Parse".
(setDate() and setTime() too)
JDBC driver : 8.1-407
URL parameter : prepareThreshold=1&loglevel=2
sample code :
create table test(id int, tm timestamp);
int n = 10;
Timestamp tm = new Timestamp(System.currentTimeMillis());
PreparedStatement pst = conn.prepareStatement("INSERT INTO test(id, tm)
VALUES (?, ?)");
for(int i=0; i< n; i++){
pst.clearParameters();
pst.setInt(1, i);
pst.setTimestamp(2, tm);
pst.executeUpdate();
}
pst.close()
----
When we used poolPreparedStatements (common-dbcp), a similar problem
seems to occur. When we called close() of PreparedStatement, dbcp side
calls clearParameters().
JDBC driver : 8.1-407
URL parameter : prepareThreshold=1&loglevel=2
Jakarta commons : commons-dbcp-1.2.1
commons-pool-1.3
commons-collections-3.2
sample code :
import org.apache.commons.dbcp.BasicDataSource;
:
String url =
"jdbc:postgresql://localhost/testdb?loglevel=2&prepareThreshold=1";
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.postgresql.Driver");
ds.setUsername("user");
ds.setPassword("passwd");
ds.setUrl(url);
ds.setPoolPreparedStatements(true);
Connection conn = ds.getConnection();
int n = 10;
Timestamp tm = new Timestamp(System.currentTimeMillis());
for(int i=0; i< n; i++){psql tes
PreparedStatement pst = conn.prepareStatement("INSERT INTO test(id,
tm) VALUES (?, ?)");
pst.setInt(1, i);
pst.setTimestamp(2, tm);
pst.executeUpdate();
pst.close()
}
----
best regards,
Akira.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: PreparedStatement clearParameters and setTimestamp
@ 2006-07-25 18:57 Kris Jurka <books@ejurka.com>
parent: Akira Sato <sato.akira@oss.ntt.co.jp>
0 siblings, 1 reply; 3+ messages in thread
From: Kris Jurka @ 2006-07-25 18:57 UTC (permalink / raw)
To: Akira Sato <sato.akira@oss.ntt.co.jp>; +Cc: pgsql-jdbc
On Tue, 25 Jul 2006, Akira Sato wrote:
> This reply is about the following threads.
> http://archives.postgresql.org/pgsql-jdbc/2006-02/msg00156.php
>
> With case of setTimestamp(), it seem to always do "Parse".
> (setDate() and setTime() too)
>
> create table test(id int, tm timestamp);
>
> int n = 10;
> Timestamp tm = new Timestamp(System.currentTimeMillis());
> PreparedStatement pst = conn.prepareStatement("INSERT INTO test(id, tm)
> VALUES (?, ?)");
> for(int i=0; i< n; i++){
> pst.clearParameters();
> pst.setInt(1, i);
> pst.setTimestamp(2, tm);
> pst.executeUpdate();
> }
> pst.close()
>
The above problem has nothing to do with the clearParameters call. You
can see this by removing it from the above test and noting that it still
parses it every time through. The real problem has to do with using oid
zero for the date+time datatypes.
When determining if a statement must be reparsed the driver checks that
the parameters it currently has match with the previous execution. This
comparison is being done with the values returned from the server for the
previous execution, not the values sent to the server. So you parse once
with oid 0 and it comes back with the oid of the resolved type, say 1184
for timestamptz. The next time through you again have oid 0 from
setTimestamp, and it compares 0 == 1184? and it determines they don't
match and it must be reparsed.
The attached patch fixes your test case for me, but I haven't applied it
yet as I'd like to do some more testing. It doesn't allows oid 0 to match
any type the server resolves.
Kris Jurka
Attachments:
dont-reparse-unspecified.patch (text/plain, 914B) download
Index: org/postgresql/core/v3/SimpleQuery.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/core/v3/SimpleQuery.java,v
retrieving revision 1.10
diff -c -r1.10 SimpleQuery.java
*** org/postgresql/core/v3/SimpleQuery.java 26 Apr 2006 20:06:53 -0000 1.10
--- org/postgresql/core/v3/SimpleQuery.java 25 Jul 2006 18:55:41 -0000
***************
*** 91,97 ****
// Check for compatible types.
for (int i = 0; i < paramTypes.length; ++i)
! if (paramTypes[i] != preparedTypes[i])
return false;
return true;
--- 91,97 ----
// Check for compatible types.
for (int i = 0; i < paramTypes.length; ++i)
! if (paramTypes[i] != 0 && paramTypes[i] != preparedTypes[i])
return false;
return true;
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: PreparedStatement clearParameters and setTimestamp
@ 2006-08-06 18:05 Kris Jurka <books@ejurka.com>
parent: Kris Jurka <books@ejurka.com>
0 siblings, 0 replies; 3+ messages in thread
From: Kris Jurka @ 2006-08-06 18:05 UTC (permalink / raw)
To: Akira Sato <sato.akira@oss.ntt.co.jp>; +Cc: pgsql-jdbc
On Tue, 25 Jul 2006, Kris Jurka wrote:
> On Tue, 25 Jul 2006, Akira Sato wrote:
>
>> With case of setTimestamp(), it seem to always do "Parse".
>> (setDate() and setTime() too)
>>
>
> When determining if a statement must be reparsed the driver checks that the
> parameters it currently has match with the previous execution. This
> comparison is being done with the values returned from the server for the
> previous execution, not the values sent to the server. So you parse once
> with oid 0 and it comes back with the oid of the resolved type, say 1184 for
> timestamptz. The next time through you again have oid 0 from setTimestamp,
> and it compares 0 == 1184? and it determines they don't match and it must be
> reparsed.
>
> The attached patch fixes your test case for me, but I haven't applied it yet
> as I'd like to do some more testing. It doesn't allows oid 0 to match any
> type the server resolves.
>
Applied to 8.0, 8.1, and 8.2dev.
Kris Jurka
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2006-08-06 18:05 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-07-25 01:42 PreparedStatement clearParameters and setTimestamp Akira Sato <sato.akira@oss.ntt.co.jp>
2006-07-25 18:57 ` Kris Jurka <books@ejurka.com>
2006-08-06 18:05 ` Kris Jurka <books@ejurka.com>
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