Results 1 to 4 of 4

Thread: General advice about transactions in Spring

Hybrid View

  1. #1
    Join Date
    Nov 2007
    Posts
    177

    Default 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. #2
    Join Date
    Apr 2011
    Posts
    107

    Default

    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
                }

  3. #3
    Join Date
    Nov 2007
    Posts
    177

    Default

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

  4. #4
    Join Date
    Apr 2011
    Posts
    107

    Default

    It is the easiest

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •