Hi Nishant,
Some pointers:
You could use the request class, which as a addMutation(Collection) option. This will go well with the kind of loop structure you have and you could have a tight code around opening a transaction, passing a collection (100 records each), and committing. The same collection will also help you with the tailing records that might not go into the commit if clause.
Request request = Request.newBuilder()
.addAllMutations(values)
.build()
The schematic of the ideal transaction is as below. Please note that there is no commit in the finally clause. Please try to follow this. I feel there is a bug in the code, and it will help you debug it out.
try {
// …
// Perform any number of queries and mutations
// …
// and finally …
txn.commit()
} catch (TxnConflictException ex) {
// Retry or handle exception.
} finally {
// Clean up. Calling this after txn.commit() is a no-op
// and hence safe.
txn.discard();
}