How to interrupt the database query using time out in Hibernate?

Asked

Viewed 184 times

1

I am developing an application that queries a gigantic database, so I would like to maintain the integrity of this allowing Hibernate to query up to 30 seconds. Faced with this problem, what would be the best way to solve it?

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 will launch an exception of the type Querytimeoutexception, that you can capture and make the necessary treatments.

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

1

It’s been a while since I did this with nhibernate. Look for references to connection properties on Hibernate. In the same file/code where you set the connection you can set the timeout. below some examples taken from ONLY

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

here using Spring

<prop key="hibernate.c3p0.idle_test_period">300</prop>
  • I am doing a native SQL query using Sqlquery, would have some problem if you set a setTimeout() for this type of query?

  • Not at first, at least no different problem as the timeout itself can be considered a problem depending on how you treat/mitigate it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.