BenMansourNizar
BenMansourNizar

Reputation: 1628

What's the difference between readOnly=true and TransactionType Never?

I'm using Spring to handle my transactions.Actually,I saw an example in which we have a method that made find on a table using default transactionn aspect @RequiredNew with readOnly=true.

    @Override
    @Transactional(readOnly = true)
    public E findAll(E entity) {
        return repository.save(entity);
    }

My question is why not making like this and what's the difference ?

@Transactional(propagation = Propagation.NEVER)
    @Override
    public Iterable<E> findAll() {
        return repository.findAll();
    }

Upvotes: 2

Views: 1003

Answers (1)

gbandres
gbandres

Reputation: 911

The readOnly property tells both Hibernate and your database that you don't want any possible changes to be committed. This sets FlushMode.NEVER in the current Hibernate Session. Even if you call a save() method, no changes will happen in your database.

Propagation.NEVER means that Spring will execute the operation non-transactionally, and will throw an Exception if a transaction exists. This ensures that no transaction will be created.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Propagation.html

Upvotes: 3

Related Questions