Search code examples
springgrailstransactional

@Transactional with 1 save statement


Does it make sense mark with spring's @Transactional a method with a single save sentence? For example:

// does it make sense mark it with @Transactional if it only has 1 save sentence?
@Transactional
public void saveMethod() {
    user.save()
}

Solution

  • If you´re using Spring data interface, you dont need use @transactional annotation. Only in case that you want to provide two execution against the database and you want to share the transaction, so then rollback of both actions can be made.

    Anyway it is always better use @transactional even as read-only for get queries(setting the FlushMode to MANUAL to let persistence providers potentially skip dirty checks when closing the EntityManager), so I would suggest put the @transactional as Service layer and read-only for get queries.