I managed to configure spring + atomikos to have global transactions across multiple databases, e.g., updating or inserting records transactionally. I have been using the @Transactional annotation together with the SqlUpdate or BatchSqlUpdate classes to perform database transactions. Here's a code example:
@Transactional
public void insertBatchIntoT1(List<Integer> ids, List<String> names) {
Map<String, Object> params = new HashMap<>();
for (int i = 0; i < ids.size(); i++) {
int id = ids.get(i);
String name = names.get(i);
params.put("id", id);
params.put("name", name);
for (BatchSqlUpdate u : insertIntoT1)
u.updateByNamedParam(params);
}
for (BatchSqlUpdate u : insertIntoT1)
u.flush();
}
The method takes a list of ids and names (it could be a list of custom POJO as well) and for each item in the list it sets the parameters in each batch update defined for each DataSource object (using a prepared statement defined at initialization) and performs the update on each. The BatchSqlUpdate is then flushed for each DataSource. I was wondering if this could be done in a different way: is it possible to incrementally add records to the BatchSqlUpdate without triggering a commit? For example, I'd like to be able to define three methods: startBatchUpdate(), addToBatch(int id,String name) and flushBatch() and have a single transaction between the first and third method call. I tried using the Propagation.REQUIRED attribute but it doesn't seem to do the trick. Any ideas? I hope I was clear in my question! Thanks