Bug 36265 - sql task: Create table fails on DB2
Summary: sql task: Create table fails on DB2
Status: RESOLVED FIXED
Alias: None
Product: Ant
Classification: Unclassified
Component: Core tasks (show other bugs)
Version: 1.6.5
Hardware: All All
: P2 normal (vote)
Target Milestone: 1.8.0
Assignee: Ant Notifications List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-08-18 23:23 UTC by Sebastien Brion
Modified: 2008-07-11 03:32 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastien Brion 2005-08-18 23:23:13 UTC
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
Comment 1 Stefan Bodewig 2008-07-11 03:32:03 UTC
svn rev 675909