TransWikia.com

What are the advantages of using timeout property in Spring framework?

Stack Overflow Asked by A.khalifa on December 1, 2021

When I read this tutorial about transaction, I notice timeout property, which I have never used before in any of REST services I have developed.

For example, in this code:

@Service
@Transactional(
  isolation = Isolation.READ_COMMITTED, 
  propagation = Propagation.SUPPORTS, 
  readOnly = false, 
  timeout = 30)
public class CarService {
 
    @Autowired
    private CarRepository carRepository;
 
    @Transactional(
      rollbackFor = IllegalArgumentException.class, 
      noRollbackFor = EntityExistsException.class,
      rollbackForClassName = "IllegalArgumentException", 
      noRollbackForClassName = "EntityExistsException")
    public Car save(Car car) {
        return carRepository.save(car);
    }
}

What is the benefit or advantage of using timeout property? is it a good practice to use it? can anyone tell me about use-cases of timeout property?

3 Answers

All of the above answers are correct, but something you should note is that:

this property exclusively designed for use with Propagation.REQUIRED or Propagation.REQUIRES_NEW since it only applies to newly started transactions.

as documentations describes.

Answered by Rasool Ghafari on December 1, 2021

As Spring Docs explain:

Timeout enables client to control how long the transaction runs before timing out and being rolled back automatically by the underlying transaction infrastructure.

So, the benefit is evidently obvious - to control how long the transaction (and queries under that) may be lasting, until they're rolled back.

Q: Why controlling the transaction time is useful/good?

A: If you are deliberately expecting your transaction not to take too long - it's a good time to use this configuration; if you're expecting that your transaction might take longer than its default maximum time, it is, agian, helpful to provide this configuration.

Answered by Giorgi Tsiklauri on December 1, 2021

  1. One is to stop records being locked for long and unable to serve any other requests.

  2. Let says you are booking a ticket. On the final submission page, it is talking so long and will your user wait forever? So you set http client time out. But now you have the http client time out, what happens if you don't have transaction time out? You displayed error to user saying it didn't succeed but your transaction takes it time as it does not have any timeout and commits after the your http client has timed out.

Answered by Kavithakaran Kanapathippillai on December 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP