Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8288

[Sql] withBatch fails when batchSize == number of addBatch call

    Details

      Description

      Sql.withBatch(batchSize, ..) calls delegate.executeBatch() both when batchCount reaches batchSize, and at the end of the method (after the call to the "batch statement" closure).

      Thus, if the number of addBatch() calls in the closure is exactly the same as, or a multiple of batchSize, one too much call to executeBatch() is made : nothing has been added to the batch.
      Which, at least with HSQLDB 2.4.0, leads to a SQLException on that very last call.

      Test to reproduce (compliant with SqlBatchTest)

      void testWithBatchSizeHavingSizeSameSizeAsStatements() {
          def numRows = sql.rows("SELECT * FROM PERSON").size()
          assert numRows == 3
          def myOthers = ['f4':'l4','f5':'l5','f6':'l6','f7':'l7']
          def result = sql.withBatch(myOthers.size(), "insert into PERSON (id, firstname, lastname) values (?, ?, ?)") { ps ->
              myOthers.eachWithIndex { k, v, index ->
                  def id = index + numRows + 1
                  ps.addBatch(id, k, v)
              }
          }
          assert result == [1] * myOthers.size()
          assert sql.rows("SELECT * FROM PERSON").size() == numRows + myOthers.size()
          // end result the same as if no batching was in place but logging should show:
          // FINE: Successfully executed batch with 4 command(s)
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                daniel_sun Daniel Sun
                Reporter:
                akapps Antoine Kapps
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: