For example this service class’ method below:
@Transactional
public void findDiscountedItems() {
List<Item> items = itemDao.findItems();
List<Item> discountedItems = new ArrayList<Item>();
for (Item i: items) {
if (isDiscounted(i))
discountedItems.add(i);
}
return discountedItems;
}
Is there any scenario it would require this method to be @Transactional?
Yes, when you perform several read operations on the database and you care about consistency. You can apply more restrictive transaction isolation level to acquire read locks and avoid problems like phantom reads.
If you run only a single query, mark the transaction as
readOnlyor even skip the@Transactionalaltogether.