1

I'm updating table using jpa (native query). I want to set timeout for the update query to run. I tried using query.setHint("javax.persistence.query.timeout", timeout), but it doesn't work. Is there any other way to add timeout ?

Below is the code snippet:

Query q = getEntityManager().createNativeQuery("some update query");
q.setParameter("personId", 1234);
int count = q.executeUpdate();
getEntityManager().flush();
return count; 
jordiburgos
  • 5,964
  • 4
  • 46
  • 80
  • _but it doesn't work_ Can you be a bit more specific? Is the returned `count` zero (0)? Or do you get some error? – Abra Jul 17 '19 at 10:48
  • What is your expected behavior if the query is not finished in that time ? – jordiburgos Jul 17 '19 at 10:50
  • @Abra, Transaction keeps on waiting until other user who uses same Id for update does not release the lock. I want it to throw TimeOutException. It should wait for maximum 5 secs and then throw exception. – hameemyousuf Jul 17 '19 at 10:55
  • check [this](https://stackoverflow.com/questions/27550663/set-timeout-on-a-typedquery-with-jpa2), also can you try `.setHint("org.hibernate.timeout", timeout)`. Similar issue on an [open ticket](https://hibernate.atlassian.net/browse/HHH-10909) on hibernate for the last 3 years – buræquete Jul 17 '19 at 10:57
  • @buræquete This also i tried earlier, but doesn't help :( – hameemyousuf Jul 17 '19 at 10:59
  • How about putting this snippet of logic in a `@Transactional(timeout = 5)` annotated method? I don't have a desktop to test this stuff on sadly, hope it helps!! – buræquete Jul 17 '19 at 11:08
  • Not working :(. Actually my first step was adding timeout in @Transactional annotation only, but i was updating in one of calling classes. Now I tried in annotated method. But no luck :( – hameemyousuf Jul 17 '19 at 11:34
  • Be aware that `@Transactional` should be added to a public method of a bean, are you sure that was the case? Not some random method. – buræquete Jul 17 '19 at 11:50

0 Answers0