groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (GROOVY-8288) [Sql] withBatch fails when batchSize == number of addBatch call
Date Tue, 15 Aug 2017 08:00:03 GMT

    [ https://issues.apache.org/jira/browse/GROOVY-8288?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16126910#comment-16126910
] 

ASF GitHub Bot commented on GROOVY-8288:
----------------------------------------

Github user akapps commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/586#discussion_r133135254
  
    --- Diff: subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlBatchTest.groovy ---
    @@ -119,6 +121,44 @@ class SqlBatchTest extends GroovyTestCase {
             // FINE: Successfully executed batch with 1 command(s)
         }
     
    +    void testWithBatchHavingSizeSameSizeAsStatements() {
    +        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)
    +    }
    +
    +    void testWithBatchNothingSpecified() {
    +        def numRows = sql.rows("SELECT * FROM PERSON").size()
    +        assert numRows == 3
    +
    +        def msg = GroovyAssert.shouldFail {
    +            sql.withBatch(3, "insert into PERSON (id, firstname, lastname) values (?,
?, ?)") { ps ->
    +                // Do nothing - not a good practice at all...
    +            }
    +        }
    +        System.err.println(msg)
    +    }
    +
    +    void testWithBatchNoPreparedStatementNothingSpecified() {
    +        def numRows = sql.rows("SELECT * FROM PERSON").size()
    +        assert numRows == 3
    +
    +        def result = sql.withBatch { ps ->
    +            // Do nothing - not a good practice at all...
    +        }
    +        assert result == [] as int[]
    --- End diff --
    
    It is quite surprising at first sight that HSQL driver behaves differently if `executeBatch()`
is called with nothing added to the batch, whether a preparedStatement is used or not.
    
    I felt like it's not Groovy responsibility to "suppress" the exception for the preparedStatement
case, and I wanted to test-specify those 2 behaviours as they are the current ones.


> [Sql] withBatch fails when batchSize == number of addBatch call
> ---------------------------------------------------------------
>
>                 Key: GROOVY-8288
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8288
>             Project: Groovy
>          Issue Type: Bug
>          Components: SQL processing
>    Affects Versions: 2.5.0-beta-1, 2.4.12
>            Reporter: Antoine Kapps
>
> {{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)
> {code:java}
> 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)
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message