3

I have doubt in placing the Spring annotation, in which layer? These are the 2 cases:

  1. case : placing @Transactional in DAO Layer

  2. case : placing @Transactional in Service Layer?

I am using Spring only, not SpringMVC.

Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
user_vs
  • 1,021
  • 3
  • 16
  • 29
  • In addition to the correct answers, there is no harm in placing the annotation on the DAO _as well_, as it will (by default) re-use the existing transaction, not open a new one. – Michael Piefel Nov 11 '14 at 09:35
  • I've often wondered if this should be actively encouraged, obviously with `required` propagation. `@Transactional` annotations broaden the unit of work when used in higher layers. Since the DAO method is the most granular unit of work possible, it stands to reason that it should also be annotated, at least assuming an RDBMS that requires transactional isolation. – Emerson Farrugia Nov 11 '14 at 09:41

3 Answers3

4

You want your services to be @Transactional. If your DAOs are transactional, and you call different DAOs in each service, then you would have multiple tansaction, which is not what you want. Make the service calls @Transactional, and all DAO calls inside those methods will participate in the transaction for the method.

Refer this link for more details

Community
  • 1
  • 1
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
3

Put it in the Service layer, because a Service may wish to access multiple DAO methods, but these would still be considered part of the same business transaction.

Stewart
  • 17,616
  • 8
  • 52
  • 80
  • if it use a single DAO, still i should @transactional in Service layer? – user_vs Nov 11 '14 at 08:56
  • Will it always use a single DAO from here on out? Never to change? – Stewart Nov 11 '14 at 08:56
  • i mean if a service use only one DAO..then i should place(@transactional) it in service layer? for any CRUD operation? – user_vs Nov 11 '14 at 09:01
  • 1
    Yes. It inherent in the entire concept of a "Service". A Service call is a complete, logical unit in the business sense, with nothing missing, nothing added. A DAO method is merely the vehicle for achieving this, and it is a mechanical consideration. In theory, a DAO could easily be swapped for something accesses a file system, or multiple DAOs which access one or more databases. – Stewart Nov 11 '14 at 09:07
  • Performance issues: That might deserve a separate question. But consider if your service method does this: (1) Access DAO. (2) Makes a HTTP call to a remote server. (3) Access another DAO. Your transaction will be open during the HTTP call, which could take up to a minute to complete. At this point, a re-think / re-design is called for. – Stewart Nov 11 '14 at 09:08
2

transaction usually means that you want to group a few operation together eg:

void bankTransfer(String fromAccount, String toAccount, BigDecimal amount)
{
if (amount.compareTo(BigDecimal.ZERO) < 0) throw new RuntimeException("hack attempt");
accountDao.deduct(fromAccount, amount);
accountDao.add(toAccount, amount);
}

here bank transfer has logic. account dao do not have logic, they merely deduct and add.

Titi Wangsa bin Damhore
  • 7,101
  • 4
  • 31
  • 36
  • +1 for being the first to provide an actual example of the relation between a Service and a DAO – Stewart Nov 11 '14 at 09:10