This happens with DB2 version 8.1, and the 8.1 db2jdbc drivers (from IBM I believe). When executing a file with simple sql create table commands, I got: CreateTables: [echo] Creating tables for user **** in jdbc:db2://database:50000/user:deferPrepares=false; driver=com.ibm.db2.jcc.DB2Driver [sql] Executing file: creates.sql [sql] Failed to execute: CREATE TABLE ACCRUAL_RULE ( ACCRUAL_RULE VARCHAR( 80) ) BUILD FAILED build.xml:2963: com.ibm.db2.jcc.b.SQLException: A result has opened by the previous getResultSet() or getUpdateCount() call, Need to call getMoreResults() Here is the stack trace of the exception: at com.ibm.db2.jcc.b.ce.getResultSet(ce.java:491) at org.apache.tools.ant.taskdefs.SQLExec.execSQL(SQLExec.java:501) at org.apache.tools.ant.taskdefs.SQLExec.runStatements(SQLExec.java:470) at org.apache.tools.ant.taskdefs.SQLExec$Transaction.runTransaction(SQLExec.java:664) It appears that the implementation of the sql task does not follow closely the jdbc spec; according to http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Statement.html#execute(java.lang.String), execute() returns whether a ResultSet is available (and getMoreResults() is similar). Therefore one should not call getResultSet() when execute() or hasMoreResult() has returned false. Similarly, calling getUpdateCount() when execute() or getMoreResults() returned true does not make sense. With the modification below, which I believe follows these rules, the problem went away. In SQLExec, I replaced: ====== BEGIN EXISTING CODE ret = statement.execute(sql); updateCount = statement.getUpdateCount(); resultSet = statement.getResultSet(); do { if (!ret) { if (updateCount != -1) { updateCountTotal += updateCount; } } else { if (print) { printResults(resultSet, out); } } ret = statement.getMoreResults(); if (ret) { updateCount = statement.getUpdateCount(); resultSet = statement.getResultSet(); } } while (ret); ==================== BEGIN REPLACEMENT CODE boolean ret; int updateCount = 0, updateCountTotal = 0; ret = statement.execute(sql); do { if (!ret) { updateCount = statement.getUpdateCount(); if (updateCount != -1) { updateCountTotal += updateCount; } } else { resultSet = statement.getResultSet(); if (print) { printResults(resultSet, out); } } ret = statement.getMoreResults(); } while (ret); ==================== END REPLACEMENT CODE
svn rev 675909