I have doubt in placing the Spring annotation, in which layer? These are the 2 cases:
case : placing
@Transactional
in DAO Layercase : placing
@Transactional
in Service Layer?
I am using Spring only, not SpringMVC.
I have doubt in placing the Spring annotation, in which layer? These are the 2 cases:
case : placing @Transactional
in DAO Layer
case : placing @Transactional
in Service Layer?
I am using Spring only, not SpringMVC.
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.
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.
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.