3

I'm using Spring Boot with Ojdbc8 18.3.0.0.0 With Hikari Datasource and JPA, all query work fine. But now I need to set Query timeout for all database query I was try many way:

javax.persistence.query.timeout=1000
spring.transaction.default-timeout=1
spring.jdbc.template.query-timeout=1
spring.jpa.properties.hibernate.c3p0.timeout=1
spring.jpa.properties.javax.persistence.query.timeout=1

Config Class:

@Configuration
public class JPAQueryTimeout {

    @Value("${spring.jpa.properties.javax.persistence.query.timeout}")
    private int queryTimeout;   

    @Bean
    public PlatformTransactionManager transactionManager() throws Exception {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setDefaultTimeout(queryTimeout); //Put 1 seconds timeout
        return txManager;
    }
}

Query:

List<Integer> llll = manager.createNativeQuery("select test_sleep(5) from dual")
            .setHint("javax.persistence.query.timeout", 1).getResultList();

The database task take 5 second before return value, but in all of case, no error occor.

Could anyone tell me how to set query timeout?

4
  • 1
    Can you try adding @Transactional(timeout = 1) on the method that is querying the db? Be aware that the method must be public. Commented Sep 20, 2019 at 2:00
  • Thank you for your anwser. I was try and it's work. The exption occur: org.hibernate.TransactionException: Unable to commit against JDBC Connection ... ORA-12592: TNS:bad packet .... Commented Sep 20, 2019 at 2:07
  • But why the exeption is: "ORA-12592: TNS:bad packet" instead of "ORA-01013: user requested cancel of current operation" ? Commented Sep 20, 2019 at 2:09
  • Hmm, that is weird, can you try to drop the usage of PlatformTransactionManager ? & use maybe proper repositories or queries with spring data jpa? & drop any other extra config you added to enforce the timeout, just use @Transactional with timeout on vanilla spring data jpa Commented Sep 20, 2019 at 2:14

1 Answer 1

2

You can try using the simplest solution, that is using the timeout value within @Transactional;

@Transactional(timeout = 1) // in seconds (so that is 1 second timeout)
public Foo runQuery(String id) {
    String result = repo.findById(id);
    // other logic
}

Be aware that the method annotated with @Transactional must be public for it to work properly

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

4 Comments

Thanks for your anwser. After using that Anonation, the query has been timeout, the exeption occur is "ORA-12592: TNS:bad packet" And then I put System.setProperty("oracle.net.disableOob", "true"); . Now it work fine with exeption throw: "ORA-01013: user requested cancel of current operation"
@user3611168 I see, I saw that solution as well, but consider dropping the weird structures like PlatformTransactionManager and using proper spring data jpa features, then you might not need to set that property
I was remove all of other config when using your suggestion,Thank you. My problem has been resolved.
how can we do it when no transaction is required but only timeout?

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.