Skip to content

Commit 6c537f3

Browse files
committed
Using batches
1 parent d6bd942 commit 6c537f3

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/main/java/com/ibm/opl/customdatasource/JdbcWriter.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
*
2525
*/
2626
public class JdbcWriter {
27+
private static long DEFAULT_BATCH_SIZE = 10000;
2728
private JdbcConfiguration _configuration;
2829
private IloOplModelDefinition _def;
2930
private IloOplModel _model;
31+
private long _batch_size;
3032

3133
/**
3234
* Convenience method to write the output of a model to a database.
@@ -44,6 +46,7 @@ public JdbcWriter(JdbcConfiguration configuration, IloOplModelDefinition def, Il
4446
_configuration = configuration;
4547
_def = def;
4648
_model = model;
49+
_batch_size = DEFAULT_BATCH_SIZE;
4750
}
4851

4952
public void customWrite() {
@@ -176,14 +179,29 @@ void customWrite(String name, String table) {
176179
String psql = getInsertQuery(schema, table);
177180
insert = conn.prepareStatement(psql);
178181
// iterate the set and create the final insert statement
182+
long icount = 1;
179183
for (java.util.Iterator it1 = tupleSet.iterator(); it1.hasNext();) {
180184
IloTuple tuple = (IloTuple) it1.next();
181185
updateValues(tuple, schema, tupleSchemaDef, insert);
182-
insert.executeUpdate();
186+
if (_batch_size == 0) {
187+
// no batch
188+
insert.executeUpdate();
189+
}
190+
else {
191+
insert.addBatch();
192+
if (icount % _batch_size == 0) {
193+
insert.executeBatch();
194+
}
195+
}
196+
icount ++;
197+
}
198+
if (_batch_size == 0) {
199+
insert.executeBatch();
183200
}
184201
conn.commit();
185202
} catch (SQLException e) {
186203
conn.rollback();
204+
throw e;
187205
} finally {
188206
if (insert != null)
189207
insert.close();

0 commit comments

Comments
 (0)