title: Why use @Transactional with @Service instead of with @Controller
body: <p>I have seen many comments in stack-overflow articles I found certain things about either <strong>@Transactional</strong> use with <strong>@Service</strong> or with <strong>@Controller</strong></p>
  <blockquote>
  <p>&quot;Usually, one should put a transaction at the service layer.&quot;</p>
  <p>&quot;The normal case would be to annotate on a service layer level&quot;</p>
  <p>&quot;Think transactions belong on the Service layer. It's the one that knows about units of work and use cases. It's the right answer if you have several DAOs injected into a Service that need to work together in a single transaction.&quot; <a href="https://stackoverflow.com/questions/1079114/spring-transactional-annotation-best-practice">[Source]</a></p>
  </blockquote>
  <p><strong>Drawback to use @transactional with @service layer</strong></p>
  <blockquote>
  <p>If I had 2 methods for example saveUser() and saveEmail() (because I store the emails in a database to send them later - like a queue) I would create in my service a method saveUserAndSendEmail(User user) which would be transactional. <a href="https://stackoverflow.com/questions/4462785/transactional-controller-vs-service">[Source]</a></p>
  </blockquote>
  <p><strong>It means I create many methods in service layer instead of one Save Generic Method as follow</strong></p>
  <pre><code>public &lt;T&gt; long save(T entity) throws DataAccessException {
      Session session = sessionFactory.getCurrentSession();
      long getGenVal=(Long) session.save(entity);
      return getGenVal;
  }
  </code></pre>
  <p><strong>According to the above solution , it means we have many methods like following LOL..</strong></p>
  <p><code>public &lt;T&gt; long saveAccount(T entity)....</code></p>
  <p><code>public &lt;T&gt; long saveWithAuditLog(T entity, K entity1)....</code></p>
  <p><code>public &lt;T&gt; long saveWithAuditLogAndEntries(T entity, K entity, M entity)....</code></p>
  <p><strong>OVERCOME this situation</strong></p>
  <p>I USE THE @Transactional in @Controller and Just make a Generic Save Method and save all the entities/ model using this simple save method. and if any method fail to save then all the transactions in controller rollback successfully.</p>
  <p><strong>Other situation that ensure that @Transactional should be use with @Controller</strong></p>
  <p>In @Controller:</p>
  <pre><code>pt.save(entity1);
  pt.save(entity2);
  int a = 2/0;
  pt.save(entity3);
  </code></pre>
  <p><strong>In case , @Transactional on Service, first 2 entity successfully saved but third not it not rollback all the transaction</strong></p>
  <p><strong>In case ,  @Transactional on @Controller all the transaction rollback as exception occur</strong></p>
  <p>why stack-overflow asked , &quot;Don't do transactions in your controller. Put them in your service layer classes.&quot;?
  <a href="https://stackoverflow.com/questions/9616410/transactional-methods-in-controller-class-are-not-considred-as-transactional">[source]</a></p>