I need to read several million rows from one database and write them to another. I would like to use PreparedStatement.addBatch
to do the writes in large batches (maybe 1000 rows). I do not need them to be in a transaction. I am writing my code in Scala 2.9.2.
One way to do this is as follows:
val sourceResultSet = ...
val targetStatement = targetConnection.prepareStatement(...)
var rowCount = 0
while (sourceResultSet.next()) {
// Read values from sourceResultSet and write them to targetStatement
targetStatement.addBatch()
rowCount += 1
if (rowCount % 1000 == 0) {
targetStatement.executeBatch()
rowCount = 0
}
}
How can I do this in a more functional way, without using a var rowCount
? I also need to consider RAM usage; I am reading several million rows, so any solution that involves have all of the source rows in memory at one time will fail.