3

Which specific exception could i catch in case Spring Data Jpa repository query gets timeout ?

Lets say that i have repository like this one:

public interface VoucherRepository extends CrudRepository<VoucherEntity, String> {

    @Transactional(readOnly = true, timeout = 30)
    VoucherEntity findByCode(String code);

    List<VoucherEntity> findAllByCodeIn(List<String> codes);
}

Which exception would be seen by caller when timeout happens ?

I don't know how to simulate this scenario, bonus question would be how to do that?

Used database is PostrgreSQL.

3 Answers 3

5

The exception you would get is QueryTimeoutException which is a subclass of the DataAccessException, which is the generic "something went wrong while using Spring to access a DB".

Sign up to request clarification or add additional context in comments.

Comments

4

If you have configured

 <jpa:repositories base-package="com.acme.repositories" />

From Spring Documentation http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.create-instances

Using this element looks up Spring Data repositories as described in Creating repository instances. Beyond that it activates persistence exception translation for all beans annotated with @Repository to let exceptions being thrown by the JPA persistence providers be converted into Spring’s DataAccessException hierarchy.

So you can catch DataAccessException to handle your exceptions

Comments

1

While using Spring's @Transactional timeout, you will get "org.springframework.transaction.TransactionSystemException" exception.

Actually you could simulate the situation by locking a table during the test like given below.

LOCK TABLE table_name IN ACCESS EXCLUSIVE MODE;

Make sure that the transaction is not complete until you complete the testing and the end the transaction to release the lock on the table.

1 Comment

If the database isn't responding at all, the exception thrown is org.springframework.transaction.CannotCreateTransactionException when using transactions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.