ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jaybytez <jayby...@gmail.com>
Subject Re: commitTransaction in a batch
Date Thu, 22 Jun 2006 00:15:32 GMT

I have not tested it, but after looking at this post, I think I may have
found the issue:

In my method below, I startBatch and then executeBatch after the first 100
records, then I never do a startBatch again.  I would assume that I have to
call startBatch again or else the first 100 records are batched but every
record after that is not batched but sent to the database individually.

    private Object batch(Object argument, SqlMapClient sqlMap, String
methodName) {
        int commitSize = 100;
        boolean commitBatch = false;
        int totalRecordsUpdated = 0;
        int commitBatchUpdated = 0;

        try {
            List list = (List)argument;
            sqlMap.startTransaction();
            sqlMap.startBatch();
           
            for(int i = 0; i < list.size(); i++) {

                sqlMap.insert(methodName, list.get(i));

                if(i == (commitSize - 1)) {
                    commitBatch = true;
                }
                else if(i == (list.size() - 1)) {
                    commitBatch = true;
                }

                if(commitBatch) {
                    commitBatchUpdated = sqlMap.executeBatch();
                    totalRecordsUpdated = totalRecordsUpdated +
commitBatchUpdated;
                    commitBatch = false;
                }
            }

            sqlMap.commitTransaction();
           
            if(totalRecordsUpdated == 0) {
                throw new DataAccessException("No records were modified by
the batch statement. " + methodName);
            }
            if(totalRecordsUpdated != list.size()) {
                throw new DataAccessException("Not all of the records were
successfully updated/inserted/deleted in batch mode. " + methodName);
            }
        }
        catch(SQLException e) {
            exceptionConverter.rethrow(methodName, e);
        }
        finally {
            try {
                sqlMap.endTransaction();
            }
            catch (SQLException e) {
                exceptionConverter.rethrow(methodName, e);
            }
        }

        return new Integer(totalRecordsUpdated);
    } 

I should be doing this in my one bit code:


                if(commitBatch) {
                    commitBatchUpdated = sqlMap.executeBatch();
                    totalRecordsUpdated = totalRecordsUpdated +
commitBatchUpdated;
                    if(i == (list.size() - 1)) {
                        sqlMap.startBatch();
                    }
                    commitBatch = false;
                }
--
View this message in context: http://www.nabble.com/executeBatch-not-returning-int-of-records-updated.-t1819686.html#a4985090
Sent from the iBATIS - User - Java forum at Nabble.com.


Mime
View raw message