Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 31AC01337BB9 for ; Wed, 16 Mar 2011 06:14:52 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 35193-01 for ; Wed, 16 Mar 2011 09:14:44 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-iw0-f174.google.com (mail-iw0-f174.google.com [209.85.214.174]) by mail.postgresql.org (Postfix) with ESMTP id EE5041337B6E for ; Wed, 16 Mar 2011 06:14:43 -0300 (ADT) Received: by iwn34 with SMTP id 34so1456384iwn.19 for ; Wed, 16 Mar 2011 02:14:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:user-agent:date:subject:from:to:message-id :thread-topic:mime-version:content-type; bh=l3f6WEEgRkqs2/YOz8XDivToM1e/duh3j7lxoyjXt/o=; b=XdSbKL76wdr/zKIRqPAxnPvT4/U40lz7CiIXoWi8K0Xhdo9NyFuQ08y+qLnZ1PkGbg JBsfBq7sC3tJ1UaZGz1xUyJwLqX7H6bKMQlPyYe/cx4PclfiZQKn6putGUlljsc0VrJg pZahL4zyoszrSyU97uAWMMBLN7ysJLqZxZXxo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=user-agent:date:subject:from:to:message-id:thread-topic :mime-version:content-type; b=wLFaNEsAUAlccwD5C3wQMvKW9drvwV7lIn8u0YgL07/MLFkBOYt4arIcQ0R9toOM2R WjMJW2CY+TSG2as1mAZwhWrWKDBNZ+uvwin0R5hf/TEwf298Xnpw3prVb2V/wBHZtt1m rUDx2NJfNEorrO+dWZ8W1WiOvGQV3lYBrNHrk= Received: by 10.42.162.196 with SMTP id z4mr659340icx.47.1300266883473; Wed, 16 Mar 2011 02:14:43 -0700 (PDT) Received: from [10.0.0.2] ([65.100.168.215]) by mx.google.com with ESMTPS id wt14sm455363icb.4.2011.03.16.02.14.41 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 16 Mar 2011 02:14:42 -0700 (PDT) User-Agent: Microsoft-MacOutlook/14.2.0.101115 Date: Wed, 16 Mar 2011 03:14:35 -0600 Subject: Problem with executeBatch and "A result was returned when none was expected" From: Sean Bowman To: Message-ID: Thread-Topic: Problem with executeBatch and "A result was returned when none was expected" Mime-version: 1.0 Content-type: multipart/alternative; boundary="B_3383090081_604969" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.885 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_ENVFROM_END_DIGIT=1.553, FREEMAIL_FROM=0.001, FREEMAIL_REPLY=1.929, HTML_MESSAGE=0.001, MIME_QP_LONG_LINE=0.001, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201103/13 X-Sequence-Number: 21568 > This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. --B_3383090081_604969 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit I've been trying to batch create a bunch of records using stored procedures and the executeBatch() method (so I can get back the generated PKID's) with no luck. I've tried PostgreSQL 8.4.2 with the 8.4-701.jar and 8.4-702.jar on OS X, PostgreSQL 9.0.3 with the latest 9.0 JAR on both OS X and Ubuntu Linux, all under JDK 1.6, and all with the same results. When I try to call executeBatch, I get a BatchUpdateException with the message: "A result was returned when none was expected". Here's my simple example that I whittled down from my larger application: import java.sql.*; public class Check { public static void main(String[] args) throws Exception { try { Class.forName("org.postgresql.Driver"); Connection con = DriverManager.getConnection( "jdbc:postgresql://localhost/mydb", "postgres", null); con.setAutoCommit(false); PreparedStatement stmt = con.prepareStatement( "INSERT INTO users (email,password,login) VALUES (?,?,?)", Statement.RETURN_GENERATED_KEYS); stmt.setObject(1, "myemail@gmail.com"); stmt.setObject(2, "test"); stmt.setObject(3, "sbowman"); stmt.addBatch(); stmt.executeBatch(); System.out.println("Got " + stmt.getGeneratedKeys()); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { System.out.println(e.getMessage()); System.out.println(e.getNextException().getMessage()); } } } This always, in all settings described above, returns (raised from the stmt.executeBatch() line): Batch entry 0 INSERT INTO users (email,password,login) VALUES ('myemail@gmail.com','test','sbowman') RETURNING * was aborted. Call getNextException to see the cause. A result was returned when none was expected. Here's the stacktrace: java.sql.BatchUpdateException: Batch entry 0 INSERT INTO users (email,password,login) VALUES ('myemail@gmail.com','test','sbowman') RETURNING * was aborted. Call getNextException to see the cause. at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(A bstractJdbc2Statement.java:2586) at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleResultR ows(AbstractJdbc2Statement.java:2550) at org.postgresql.core.v3.QueryExecutorImpl$1.handleResultRows(QueryExecutorImp l.java:437) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.ja va:1747) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statem ent.java:2725) at Check.main(Check.java:21) I think my code is correct. If I modify the above code to use a MySQL database instead of a PostgreSQL database, everything works fine and I do get the PKID's generated by the database back. I was under the impression that getGeneratedKeys() was now working, since 8.4-701.jar, but for the life of me I can't figure out how. If I skip using the batch methods and insert user records one at a time, calling "stmt.execute()", then the getGeneratedKeys() call returns a ResultSet with the generated ID in it. So execute() works, but executeBatch() doesn't. Any suggestions or pointing out my code flaws would be most appreciated. Or does executeBatch just not work? I've been battling this issue most of the day and I'm just about at my wits' end. Should I just give up and switch to calling execute() one at a time? Seems far less efficient, but at least it seems to work. Thanks, Sean --B_3383090081_604969 Content-type: text/html; charset="US-ASCII" Content-transfer-encoding: quoted-printable
I've been trying to batch cr= eate a bunch of records using stored procedures and the executeBatch() metho= d (so I can get back the generated PKID's) with no luck.  I've tried Po= stgreSQL 8.4.2 with the 8.4-701.jar and 8.4-702.jar on OS X, PostgreSQL 9.0.= 3 with the latest 9.0 JAR on both OS X and Ubuntu Linux, all under JDK 1.6, = and all with the same results.  When I try to call executeBatch, I get = a BatchUpdateException with the message:  "A result was returned when n= one was expected".

Here's my simple example that I = whittled down from my larger application:  

import java.sql.*;

=
public class Check {
    public static v= oid main(String[] args) throws Exception {
     &nb= sp;  try {
            Cla= ss.forName("org.postgresql.Driver");
       &n= bsp;    Connection con =3D DriverManager.getConnection(
&n= bsp;                   "jd= bc:postgresql://localhost/mydb", "postgres", null);

            con.setAutoCommit(false= );

            Prepar= edStatement stmt =3D con.prepareStatement(
      = ;              "INSERT INTO users (email,= password,login) VALUES (?,?,?)",
        =            Statement.RETURN_GENERATED_KEYS);<= /div>

            stmt= .setObject(1, "myemail@gmail.com");
       &nb= sp;    stmt.setObject(2, "test");
     &n= bsp;      stmt.setObject(3, "sbowman");

<= div>            stmt.addBatch();
            stmt.executeBatch();<= div>
            System.out= .println("Got " + stmt.getGeneratedKeys());
     &n= bsp;  } catch (ClassNotFoundException e) {
    = ;        e.printStackTrace();
   &nb= sp;    } catch (SQLException e) {
     &n= bsp;      System.out.println(e.getMessage());
 = ;           System.out.println(e.getNextExcept= ion().getMessage());
        }
=     }
}

This always, in all settings described above, returns (rais= ed from the stmt.executeBatch() line):

Batc= h entry 0 INSERT INTO users (email,password,login) VALUES ('myemail@gmail.co= m','test','sbowman') RETURNING * was aborted.  Call getNextException to= see the cause.
A result was returned when none was expected.

Here's the stacktrace:

at org.postgresql.jdbc2.AbstractJdbc2Statem= ent$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2586)<= /font>
at org.postgresql.jdbc2.Abs= tractJdbc2Statement$BatchResultHandler.handleResultRows(AbstractJdbc2Stateme= nt.java:2550)
<= /font>at org= .postgresql.core.v3.QueryExecutorImpl$1.handleResultRows(QueryExecutorImpl.j= ava:437)
at org.post= gresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1747)=
= at org.postgresql.c= ore.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
at org.postgresql.jdbc2.AbstractJdb= c2Statement.executeBatch(AbstractJdbc2Statement.java:2725)
at Check.main(Check.java:21)

I think my code is correct. &nbs= p;If I modify the above code to use a MySQL database instead of a PostgreSQL= database, everything works fine and I do get the PKID's generated by the da= tabase back.  

I was under the impression that= getGeneratedKeys() was now working, since 8.4-701.jar, but for the life of = me I can't figure out how.

If I skip using the batc= h methods and insert user records one at a time, calling "stmt.execute()", t= hen the getGeneratedKeys() call returns a ResultSet with the generated ID in= it.  So execute() works, but executeBatch() doesn't.

Any suggestions or pointing out my code flaws would be most apprecia= ted.  Or does executeBatch just not work?  I've been battling this= issue most of the day and I'm just about at my wits' end.  Should I ju= st give up and switch to calling execute() one at a time?  Seems far le= ss efficient, but at least it seems to work.

Thanks= ,
Sean

--B_3383090081_604969--