0

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

Aacini
  • 65,180
  • 12
  • 72
  • 108
Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94
  • Hmmm. Looks like what you want is the XA protocol aka 2 phase commits: http://en.wikipedia.org/wiki/Two-phase_commit_protocol http://www.atomikos.com/Documentation/WhatIsXa – Frederic Conrotte Jan 29 '13 at 10:22
  • No, this is what I'm using already (as per the Transactional annotation). I am just wondering if I could avoid the need to pass a list. The only solution I found so far is to store records in a buffer and then when the batch is complete call a transactional method that takes data from the buffer and commits the updates/inserts. – Giovanni Botta Jan 29 '13 at 16:18

1 Answers1

0

What prevents you from extracting those 3 methods and call them from within insertBatchIntoT1?

Guy Pardon
  • 484
  • 2
  • 8