Announcement Announcement Module
Collapse
No announcement yet.
General advice about transactions in Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.

  • #2
    You can create a new method (in another service so that proxy transaction will work):

    Code:
    public class AbonneService{
    ...
        @Transactional
        public void manageMailing(Abonne abonne, List<Sculpture> sculpturesChoisiesPourMailing){
    
           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);
           }
    }
    Then, your main service will look like:
    Code:
      for (Abonne abonne : totaliteAbonnes) {
                   abonneService.manageMailing(...);
                    compteursMailsEnvoyes++;
                    //todo: transaction ends here
                }

    Comment


    • #3
      Thanks for your reply gwa,
      Is that the preferred way over programmatic transaction management?
      Regards,
      J.

      Comment


      • #4
        It is the easiest

        Comment

        Working...
        X