ayengin
ayengin

Reputation: 1596

Spring @transactional annotation usage

If i don't put @Transactional annotation on limsservice' audit metod, audit log not persisted .But persist metod in persistenceService has allready this annotation.Why i have to put this annotation.

  @RequestMapping(value = "/lemf", method = RequestMethod.POST, headers = "Accept=application/json")
    public
    @ResponseBody
    Lemf create(HttpServletResponse response, @RequestBody Lemf lemf) {
        try {
            Lemf l = limsService.findLemfByName(lemf.getName());
            if (l == null) {
                     lemf=   limsService.addLemf(lemf);
                    limsService.audit("User action",getRequestInfo(),"New lemf created.");
                     return  lemf;
            }
            sendError(response, "Lemf Allready exist.");


        } catch (ProviderException e) {
            sendError(response, e);
        }
        return null;
    }


public class PersistenceServiceImpl implements PersistenceService {

   @Transactional(readOnly = false)
    public <T extends Persistable> T persist(T obj) {
        if (obj.getSystemId() != AbstractPersistable.UNSAVED_ID_VALUE) {
            log.info("Updating... :" + obj);
            T merged = em.merge(obj);
            return merged;
        } else {
            em.persist(obj);
            return obj;
        }
    }

}


public class LimsServiceImpl implements LimsService {
  @Override
    public Lemf addLemf(Lemf lemf) throws ProviderException {
        return  persistenceService.persist(lemf);
    }

    @Transactional
    public void  audit(String type,Pair<String,String >  pair,String log)  {
         auditService. audit("User action", pair, "New lemf created.");
    }

}

Upvotes: 3

Views: 7493

Answers (1)

Ralph
Ralph

Reputation: 120861

Spring Reference chapter 10.5.6. Using @Transactional

Is very clear about this:

Tip

Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Transactional annotation, as opposed to annotating interfaces. You certainly can place the @Transactional annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies. The fact that Java annotations are not inherited from interfaces means that if you are using class-based proxies (proxy-target-class="true") or the weaving-based aspect (mode="aspectj"), then the transaction settings are not recognized by the proxying and weaving infrastructure, and the object will not be wrapped in a transactional proxy, which would be decidedly bad.

Upvotes: 1

Related Questions