title: Domain driven design and transactions in Spring environment
body: <p>I used to design my application around anemic domain model, so I had many repository object, which were injected to the big, fat, transaction-aware service layer. This pattern is called Transaction script. It's not considered a good practice since it leads to the procedural code, so I wanted to move forward to the domain driven design.</p>
  
  <p>After reading couple of articles on the web, listening to the Chris Richardson's talk on Parleys and reading DDD chapters of the POJOs in Action, I think I got the big picture.</p>
  
  <p>Problem is, that I don't know, how to organize transactions in my application. Chis Richardson in his book states:</p>
  
  <blockquote>
    <p>The presentation tier handles HTTP requests from the user’s browser by calling
    the domain model either directly or indirectly via a façade, which as I
    described in the previous chapter is either a POJO or an EJB.</p>
  </blockquote>
  
  <p>Good so far, but Srini Penchikala on <a href="http://www.infoq.com/articles/ddd-in-practice" rel="noreferrer">InfoQ article</a> states:</p>
  
  <blockquote>
    <p>Some developers prefer managing the transactions in the DAO classes which is a poor design. This results in too fine-grained transaction control which doesn't give the flexibility of managing the use cases where the transactions span multiple domain objects. Service classes should handle transactions; this way even if the transaction spans multiple domain objects, the service class can manage the transaction since in most of the use cases the Service class handles the control flow.</p>
  </blockquote>
  
  <p>Ok, so if I understand this correctly, repository classes should not be transactional, service layer (which is now much thinner) is transactional (as it used to be in Transaction script pattern). But what if domain objects are called by presentation layer directly? Does it mean, that my domain object should have transactional behavior? And how to implement it in Spring or EJB environment?</p>
  
  <p>This seems kind of weird to me, so I'd be happy if somebody would clarify that. Thank you.</p>