Kumar Abhishek
Kumar Abhishek

Reputation: 192

Set timeout based on query

How to set up a timeout for some specific queries using Java or Hibernate. The problem I am facing is that some queries where I have to fetch large amount of data some times do not respond for longer periods resulting in an unresponsive application.

Upvotes: 1

Views: 2874

Answers (3)

Neil Stockton
Neil Stockton

Reputation: 11531

JPA standard property javax.persistence.query.timeout can also be provided as a "hint" to each query.

query.setHint("javax.persistence.query.timeout", someTimeout);

Hence avoiding tying your code to one JPA provider or another.

Upvotes: 1

The Roy
The Roy

Reputation: 2208

Hibernate supports Transaction time outs:

    //set transaction timeout to 5 seconds at session
    Session sess = factory.openSession();
    sess.getTransaction().setTimeout(5);

You can also setup at query level.

Query q = sess.createQuery("from....")
          .setTimeOut(3);

Upvotes: 0

StanislavL
StanislavL

Reputation: 57381

Hibernate query has setTimeout() method

Query query = session.createQuery(someQueryString);
query.setTimeout(theTimeOut);

Upvotes: 2

Related Questions