I’m using Spring simplejdbctemplate for bunch of insert/update based on if/else blocks.
I’m wondering whether there is a way to combine all these operations into one transaction. Currently, if my code breaks (for some reason) then certain inserts are executed and certain are not. I would like the whole thing to fail, if anything fails. Like rollback in SQL.
Is this possible to do with simplejdbctemplate?
The link does not not help me much as I don’t understand where I can place @Transaction. I am pasting the code below, Where would @Transaction annotations go in the code below?
Update
code:
for (Colors c : colors) {
if (isColorExistsInOtherDb(c)) {
if (!isColorExistsAlready(c)) {
insertIntoColor(c);
colorId = getMaxColorId();
}
else {
updateColor(c);
colorId = getColorIdByShade(c);
}
for (Shade s : c.getShades()) {
colorId = colorService.isShadeExistsForColor(colorId, s.getShadeId());
if (colorId <= 0) {
colorService.insertIntoColor(s);
colorId = colorService.getMaxColorId();
}
else {
colorService.updateColor(colorId, c);
}
insertMachinePoam(machineId, poamId);
}
}
else {
//do something else?
}
Simple Jdbc Template doesn’t control whether you are in a transaction. This page shows an example of using annotations to control transaction settings.
In general, you are making the multiple DAO/jdbc template calls from a service type method. That method gets annotated for transactions so your jdbc template code can focus on what it is designed to do – access the database.