You are on page 1of 2

The place to demarcate transactions in a Spring enabled application is service layer, nowhere else.

You
should only mark

@Service beans as @Transactional or their public methods.

@Service

public class SecurityServiceImpl implements SecurityService {

@Autowired

private SecurityDao securityDao;

@Override

@Transactional(readOnly=true)

public User findUserByUsername(String username) {

//...

@Override

@Transactional

public void createUser(User user) {

//...

@Override

@Transactional

public void updateUser(User user) {

//...

@Override

@Transactional

public void deleteUser(User user) {

//...

You can still place @Transactional with propagation=Propagation.MANDATORY over DAO classes so that
they wouldn’t be

accessed without an active transaction at all.


1.16 Mark transactions as readOnly=true when service methods only contain queries

In order to be able to use Hibernate contextual session capability, you need to start a transaction even for
select operations.

Therefore, you even mark your finder methods with @Transactional annotation in service beans. However,
at the end of the

finder method, transaction is committed, and Hibernate session flush will be triggered via that commit.
Hibernate flush is an

expensive operation, which traverses all those entities existing in the Hibernate Session, and try to detect
dirty entities within it.

You might also like