General advice about transactions in Spring
Hello,
I have the following method (notice the comments //todo: transaction starts here, etc.):
Code:
public int traiterMailing(/*List<Sculpture> sculpturesChoisiesPourMailing*/) {//todo: gerer le cas oł zero sculptures et gerer le cas oł une adresse email est invalide.
log.debug("traiterMailing");
List<Sculpture> sculpturesChoisiesPourMailing = dao.findMailingNonTraitees();//todo: temp?
List<Abonne> totaliteAbonnes = dao.findAllAbonnes();
int compteursMailsEnvoyes = 0;
if (sculpturesChoisiesPourMailing.isEmpty()) {
return compteursMailsEnvoyes;//todo: threadsafe!!
} else {
for (Abonne abonne : totaliteAbonnes) {
//todo: transaction starts here
for (Sculpture sculpture : sculpturesChoisiesPourMailing) {
MailingAbonnePK mapk = new MailingAbonnePK(sculpture.getSculptureID(), abonne.getAbonneID());
MailingAbonne ma = new MailingAbonne(mapk, new Date());
dao.persistMailingAbonnee(ma);
}
this.mailAbonne(sculpturesChoisiesPourMailing, abonne);
compteursMailsEnvoyes++;
//todo: transaction ends here
}
List<Integer> sculpturesChoisiesPourMailingIDs = new ArrayList<Integer>();
for (Sculpture sculpture : sculpturesChoisiesPourMailing) {
sculpturesChoisiesPourMailingIDs.add(sculpture.getSculptureID());
}
dao.updateSculptureChoisiesPourMailing(sculpturesChoisiesPourMailingIDs);
return compteursMailsEnvoyes;
}
}
I wish to use the equivalent of tx.begin() and tx.commit() respectively at //transaction starts here and //transaction ends here.
Can anyone advise which Spring class to use or whether I should refactor my method and if so how?
Thanks in advance,
J.