I have got a method where it will update two tables (table A and table B). I would want the whole transaction to be atomic so that if updating table A failed (either the database update failed, or the code thrown exception before making the actual database call), then it will not update table B so as to preserve data consistency. My questions:
I think of using
@Transactionalannotation to do so, is this usage correct ?I have seen some placing the
@Transactionalat both the class level and also the method level, if I only put at the method level, will it still work ?Suppose for the following. If I only place
@Transactionalin the private method ofupdateValue, does it mean only the update action fromtableADao.updateValue(value)andtableBDao.updateValue(value)(but nottableADao.get(userId)) will be wrapped inside one transaction ?
public void updateMethod(final Long userId, Integer value) {
//some processing
Integer value = tableADao.get(userId);
//some processing
updateValue(value);
//some processing
}
@Transactional
private void updateValue(final Integer value) {
tableADao.updateValue(value);
tableBDao.updateValue(value);
}