How to stop database query using time out in hibernate?

1

I'm developing an application that queries a gigantic database, so I'd like to maintain its integrity by allowing Hibernate to query up to 30 seconds. In the face of this problem, what would be the best way to solve it?

    
asked by anonymous 19.02.2016 / 17:58

2 answers

2

Complementing Jean's answer, in hibernate you have the method setTimeout () where you can set the maximum time a particular query will be able to execute.

Example:

Query suaQuery;
// Tempo do timeout e sua unidade
suaQuery.setTimeout(10,TimeUnit.MILLISECONDS);

If the query takes longer than the set time, it will throw an exception of type QueryTimeoutException , which you can capture and make the necessary treatments.

try {
    suaQuery.list();
}
catch (QueryTimeoutException e) {
    // faz qualquer tratamento que seja necessário
}
    
19.02.2016 / 18:13
1

It's been a while since I've done this with nHibernate. Look for references to connection properties in hibernate. In the same file / code where you define the connection you can set the timeout. here are some examples taken from SO

<property name="hibernate.c3p0.timeout">300</property>

here using Spring

<prop key="hibernate.c3p0.idle_test_period">300</prop>
    
19.02.2016 / 18:06