Skip to Content
0
Former Member
Jun 11, 2014 at 01:48 PM

Why do I get a problem if I set a batch size for prepared statement to 1?

224 Views

Hi,

Is it a bug?

I use the following pseudo code to send an array of data records to one database:

  
  
  1. ...
  2. var myDataRecords = [...]; // array with data records
  3. var myInsertQuery = 'INSET INTO ...';
  4. prepSt = conn.prepareStatement(myInsertQuery);
  5. if (myDataRecords.length > 0){
  6. prepSt.setBatchSize(myDataRecords.length);
  7. for(var i = 0; i < myDataRecords.length; i++){
  8. prepSt.setInt(1, myDataRecords[i].dataField_1);
  9. prepSt.setString(2, myDataRecords[i].dataField_2);
  10. ...
  11. prepSt.setString(n, myDataRecords[i].dataField_n);
  12. prepSt.addBatch();
  13. }
  14. prepSt.executeBatch();
  15. }
  16. prepSt.close();
  17. ...

It works fine but if the data record array has only one record this code throws exception at addBatch():

"PreparedStatement.addBatch: bulk insert not enabled".

Is a "batching" with a batch size = 1 a special case? The special cases make a code ugly. 😔.