pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Multiple Statement result set problem with PreparedStatements ?
7+ messages / 2 participants
[nested] [flat]

* Multiple Statement result set problem with PreparedStatements ?
@ 2005-07-14 00:50 Lenard, Rohan (Rohan) <rlenard@avaya.com>
  2005-07-14 01:40 ` Re: Multiple Statement result set problem with PreparedStatements Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Lenard, Rohan (Rohan) @ 2005-07-14 00:50 UTC (permalink / raw)
  To: pgsql-jdbc

I have SQL like this (foo has a pkey from a serial) -
 
INSERT INTO foo ( a, b ) VALUES ( ?, ? );
INSERT INTO bar ( foo_fkey, c ) VALUES ( ?, currval('foo_seq'::text) );
SELECT currval('bar_seq'::text) AS x, currval('foo_seq'::text) AS y;
 
that I'm using like this
 
final PreparedStatement stmt = conn.createPreparedStatement(" 
INSERT INTO foo ( a, b ) VALUES ( ?, ? );
INSERT INTO bar ( foo_fkey, c ) VALUES ( ?, currval('foo_seq'::text) );
SELECT currval('bar_seq'::text), currval('foo_seq'::text);");
 
stmt.setString(1, "A");
stmt.setString(2, "B");
stmt.setString(3, "C");
 
boolean result = stmt.execute();
if (result) {
  processRS(stmt.getResultSet());
}
else {
  if (getMoreResults()) {
     processRS(stmt.getResultSet()); // only gets called with 1 element
SELECTED.
  }
}
 
This works fine if the SELECT returns 1 value only, but as soon as it
returns 2 values the inserts work but no results are given... What the
???
 
I have pg-8.0-311 against a 7.3.10 server..
 
 
Any ideas  ?
 
Thx,
    Rohan
 
 

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Multiple Statement result set problem with PreparedStatements
  2005-07-14 00:50 Multiple Statement result set problem with PreparedStatements ? Lenard, Rohan (Rohan) <rlenard@avaya.com>
@ 2005-07-14 01:40 ` Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Oliver Jowett @ 2005-07-14 01:40 UTC (permalink / raw)
  To: Lenard, Rohan (Rohan) <rlenard@avaya.com>; +Cc: pgsql-jdbc

Lenard, Rohan (Rohan) wrote:

> final PreparedStatement stmt = conn.createPreparedStatement("
> INSERT INTO foo ( a, b ) VALUES ( ?, ? );
> INSERT INTO bar ( foo_fkey, c ) VALUES ( ?, currval('foo_seq'::text) );
> SELECT currval('bar_seq'::text), currval('foo_seq'::text);");
>  
> stmt.setString(1, "A");
> stmt.setString(2, "B");
> stmt.setString(3, "C");
>  
> boolean result = stmt.execute();
> if (result) {
>   processRS(stmt.getResultSet());
> }

That should handle the first INSERT.

> else {
>   if (getMoreResults()) {
>      processRS(stmt.getResultSet()); // only gets called with 1 element
> SELECTED.
>   }
> }

That should handle the second INSERT.

You do not appear to be handling the results of the SELECT at all.

Your testcase is not very clear about what you're trying to do and the
incorrect behaviour you see :/

-O



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Multiple Statement result set problem with PreparedStatements ?
@ 2005-07-14 01:45 Lenard, Rohan (Rohan) <rlenard@avaya.com>
  2005-07-14 02:03 ` Re: Multiple Statement result set problem with PreparedStatements Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Lenard, Rohan (Rohan) @ 2005-07-14 01:45 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc

What I'm trying to do is do 2 inserts and get the resultant key values
from the 2 inserts - i.e. effectively the row indexes (there are
separate sequences for the pkey for each table, so the currval() gives
the key of the insert for that table.

This SQL works fine from any command line interface to the DB - giving
the expected results.

However through the JDBC driver, I don't get any results with a select
involving the 2 seqs, but if I choose just one I do !!. 

BTW - The followup analysis below is flawed.

With this SQL the driver always returns false for the execute() thus
taking the getMoreResults() path & there are never results for the
INSERT (at least not easily obtainable via the API in a generic way).

Rohan
| -----Original Message-----
| From: Oliver Jowett [mailto:oliver@opencloud.com] 
| Sent: Thursday, July 14, 2005 11:40 AM
| To: Lenard, Rohan (Rohan)
| Cc: pgsql-jdbc@postgresql.org
| Subject: Re: [JDBC] Multiple Statement result set problem 
| with PreparedStatements ?
| 
| Lenard, Rohan (Rohan) wrote:
| 
| > final PreparedStatement stmt = conn.createPreparedStatement("
| > INSERT INTO foo ( a, b ) VALUES ( ?, ? ); INSERT INTO bar ( 
| foo_fkey, 
| > c ) VALUES ( ?, currval('foo_seq'::text) ); SELECT 
| > currval('bar_seq'::text), currval('foo_seq'::text);");
| >  
| > stmt.setString(1, "A");
| > stmt.setString(2, "B");
| > stmt.setString(3, "C");
| >  
| > boolean result = stmt.execute();
| > if (result) {
| >   processRS(stmt.getResultSet());
| > }
| 
| That should handle the first INSERT.
| 
| > else {
| >   if (getMoreResults()) {
| >      processRS(stmt.getResultSet()); // only gets called with 1 
| > element SELECTED.
| >   }
| > }
| 
| That should handle the second INSERT.
| 
| You do not appear to be handling the results of the SELECT at all.
| 
| Your testcase is not very clear about what you're trying to 
| do and the incorrect behaviour you see :/
| 
| -O
| 



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Multiple Statement result set problem with PreparedStatements
  2005-07-14 01:45 Re: Multiple Statement result set problem with PreparedStatements ? Lenard, Rohan (Rohan) <rlenard@avaya.com>
@ 2005-07-14 02:03 ` Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Oliver Jowett @ 2005-07-14 02:03 UTC (permalink / raw)
  To: Lenard, Rohan (Rohan) <rlenard@avaya.com>; +Cc: pgsql-jdbc

Lenard, Rohan (Rohan) wrote:
> What I'm trying to do is do 2 inserts and get the resultant key values
> from the 2 inserts - i.e. effectively the row indexes (there are
> separate sequences for the pkey for each table, so the currval() gives
> the key of the insert for that table.
> 
> This SQL works fine from any command line interface to the DB - giving
> the expected results.
> 

> However through the JDBC driver, I don't get any results with a select
> involving the 2 seqs, but if I choose just one I do !!. 

That is weird.

I will try to find time to put together a selfcontained testcase later
on (you could speed this up by sending me one :)

> BTW - The followup analysis below is flawed.
> 
> With this SQL the driver always returns false for the execute() thus
> taking the getMoreResults() path & there are never results for the
> INSERT (at least not easily obtainable via the API in a generic way).

Sorry -- not thinking there :)

What I'd expect to see is this sequence:

  execute() returns false (no resultset for this result)
  getUpdateCount() returns 1 (1 row inserted)
  getMoreResults() returns false (no resultset for this result)
  getUpdateCount() returns 1 (1 row inserted)
  getMoreResults() returns true (resultset present)
  getResultSet() returns an appropriate resultset for the SELECT
  getMoreResults() returns false (no resultset for this result)
  getUpdateCount() returns -1 (no more results)

Your code seems to be expecting something different..

-O



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Multiple Statement result set problem with PreparedStatements ?
@ 2005-07-14 02:05 Lenard, Rohan (Rohan) <rlenard@avaya.com>
  2005-07-14 02:19 ` Re: Multiple Statement result set problem with PreparedStatements Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Lenard, Rohan (Rohan) @ 2005-07-14 02:05 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc

I have one but it relies on particular tables existing - if I get a few
minutes I too will simplify it to cause it ..

Rohan
| -----Original Message-----
| From: Oliver Jowett [mailto:oliver@opencloud.com] 
| Sent: Thursday, July 14, 2005 12:04 PM
| To: Lenard, Rohan (Rohan)
| Cc: pgsql-jdbc@postgresql.org
| Subject: Re: [JDBC] Multiple Statement result set problem 
| with PreparedStatements ?
| 
| Lenard, Rohan (Rohan) wrote:
| > What I'm trying to do is do 2 inserts and get the resultant 
| key values 
| > from the 2 inserts - i.e. effectively the row indexes (there are 
| > separate sequences for the pkey for each table, so the 
| currval() gives 
| > the key of the insert for that table.
| > 
| > This SQL works fine from any command line interface to the 
| DB - giving 
| > the expected results.
| > 
| 
| > However through the JDBC driver, I don't get any results 
| with a select 
| > involving the 2 seqs, but if I choose just one I do !!.
| 
| That is weird.
| 
| I will try to find time to put together a selfcontained 
| testcase later on (you could speed this up by sending me one :)
| 
| > BTW - The followup analysis below is flawed.
| > 
| > With this SQL the driver always returns false for the 
| execute() thus 
| > taking the getMoreResults() path & there are never results for the 
| > INSERT (at least not easily obtainable via the API in a 
| generic way).
| 
| Sorry -- not thinking there :)
| 
| What I'd expect to see is this sequence:
| 
|   execute() returns false (no resultset for this result)
|   getUpdateCount() returns 1 (1 row inserted)
|   getMoreResults() returns false (no resultset for this result)
|   getUpdateCount() returns 1 (1 row inserted)
|   getMoreResults() returns true (resultset present)
|   getResultSet() returns an appropriate resultset for the SELECT
|   getMoreResults() returns false (no resultset for this result)
|   getUpdateCount() returns -1 (no more results)
| 
| Your code seems to be expecting something different..
| 
| -O
| 



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Multiple Statement result set problem with PreparedStatements
  2005-07-14 02:05 Re: Multiple Statement result set problem with PreparedStatements ? Lenard, Rohan (Rohan) <rlenard@avaya.com>
@ 2005-07-14 02:19 ` Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Oliver Jowett @ 2005-07-14 02:19 UTC (permalink / raw)
  To: Lenard, Rohan (Rohan) <rlenard@avaya.com>; +Cc: pgsql-jdbc

Lenard, Rohan (Rohan) wrote:
> I have one but it relies on particular tables existing - if I get a few
> minutes I too will simplify it to cause it ..

Hm, ok, try the attached testcase. It works fine for me here against
8.0.1 and 7.3.8 servers:

> oliver@extrashiny ~/pgjdbc-test $ java -classpath .:/home/oliver/pgjdbc/jars/postgresql.jar TestMultiInsert 'jdbc:postgresql://localhost:5432/test?user=oliver'
> Result #1 has resultset? false update count 1
> Result #2 has resultset? false update count 1
> Result #3 has resultset? true update count -1
> oliver@extrashiny ~/pgjdbc-test $ java -classpath .:/home/oliver/pgjdbc/jars/postgresql.jar TestMultiInsert 'jdbc:postgresql://localhost:5738/test?user=oliver'
> Result #1 has resultset? false update count 1
> Result #2 has resultset? false update count 1
> Result #3 has resultset? true update count -1

Those are the results I was expecting, anyway.. were you expecting
something different?

-O

Attachments:

TestMultiInsert.java (text/plain, 1.3K) download import java.sql.*; // Run with one argument: a JDBC url to connect to. public class TestMultiInsert { public static void main(String[] args) throws Exception { Class.forName("org.postgresql.Driver"); Connection c = DriverManager.getConnection(args[0]); c.setAutoCommit(false); Statement s = c.createStatement(); s.executeUpdate("CREATE SEQUENCE seq_i"); s.executeUpdate("CREATE SEQUENCE seq_j"); s.executeUpdate("CREATE TEMP TABLE t1 (i int4 default nextval('seq_i'), zz text)"); s.executeUpdate("CREATE TEMP TABLE t2 (j int4 default nextval('seq_j'), i int4, zz text)"); PreparedStatement ps = c.prepareStatement("INSERT INTO t1(zz) VALUES (?); INSERT INTO t2(i,zz) VALUES(currval('seq_i'),?); SELECT currval('seq_i'), currval('seq_j')"); ps.setString(1, "A"); ps.setString(2, "B"); boolean resultType = ps.execute(); int updateCount = ps.getUpdateCount(); int num = 1; do { System.err.println("Result #" + num + " has resultset? " + resultType + " update count " + updateCount); ++num; resultType = ps.getMoreResults(); updateCount = ps.getUpdateCount(); } while (resultType || updateCount != -1); s.close(); c.rollback(); c.close(); } }
^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: Multiple Statement result set problem with PreparedStatements ?
@ 2005-07-14 02:34 Lenard, Rohan (Rohan) <rlenard@avaya.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Lenard, Rohan (Rohan) @ 2005-07-14 02:34 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc

Okay - my bad - that works and I think I can see what I did wrong.  I
have to call "both" methods to get the right results..

That's something that should be in the documentation.  The JDBC javadoc
doesn't make it obvious you really need to get it right so call them
both - it says call one or the other ...

| -----Original Message-----
| From: Oliver Jowett [mailto:oliver@opencloud.com] 
| Sent: Thursday, July 14, 2005 12:20 PM
| To: Lenard, Rohan (Rohan)
| Cc: pgsql-jdbc@postgresql.org
| Subject: Re: [JDBC] Multiple Statement result set problem 
| with PreparedStatements ?
| 
| Lenard, Rohan (Rohan) wrote:
| > I have one but it relies on particular tables existing - if I get a 
| > few minutes I too will simplify it to cause it ..
| 
| Hm, ok, try the attached testcase. It works fine for me here against
| 8.0.1 and 7.3.8 servers:
| 
| > oliver@extrashiny ~/pgjdbc-test $ java -classpath 
| .:/home/oliver/pgjdbc/jars/postgresql.jar TestMultiInsert 
| 'jdbc:postgresql://localhost:5432/test?user=oliver'
| > Result #1 has resultset? false update count 1 Result #2 has 
| resultset? 
| > false update count 1 Result #3 has resultset? true update count -1 
| > oliver@extrashiny ~/pgjdbc-test $ java -classpath 
| .:/home/oliver/pgjdbc/jars/postgresql.jar TestMultiInsert 
| 'jdbc:postgresql://localhost:5738/test?user=oliver'
| > Result #1 has resultset? false update count 1 Result #2 has 
| resultset? 
| > false update count 1 Result #3 has resultset? true update count -1
| 
| Those are the results I was expecting, anyway.. were you 
| expecting something different?
| 
| -O
| 



^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2005-07-14 02:34 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-07-14 00:50 Multiple Statement result set problem with PreparedStatements ? Lenard, Rohan (Rohan) <rlenard@avaya.com>
2005-07-14 01:40 ` Re: Multiple Statement result set problem with PreparedStatements Oliver Jowett <oliver@opencloud.com>
2005-07-14 01:45 Re: Multiple Statement result set problem with PreparedStatements ? Lenard, Rohan (Rohan) <rlenard@avaya.com>
2005-07-14 02:03 ` Re: Multiple Statement result set problem with PreparedStatements Oliver Jowett <oliver@opencloud.com>
2005-07-14 02:05 Re: Multiple Statement result set problem with PreparedStatements ? Lenard, Rohan (Rohan) <rlenard@avaya.com>
2005-07-14 02:19 ` Re: Multiple Statement result set problem with PreparedStatements Oliver Jowett <oliver@opencloud.com>
2005-07-14 02:34 Re: Multiple Statement result set problem with PreparedStatements ? Lenard, Rohan (Rohan) <rlenard@avaya.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