TypedQuery上设置超时

TypedQuery上设置超时

本文介绍了使用JPA2在TypedQuery上设置超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在javax.persistence.TypedQuery上设置超时。



我发现了这种简单的方法:

  TypedQuery< Foo>查询= ...; 
query.setHint( javax.persistence.query.timeout,1000);
query.getReturnList();

但似乎不起作用,只是被忽略了。

PRO JPA 2 书中:

有人知道其他方法可以在TypedQuery上指定超时吗?



使这个提示起作用?



谢谢



编辑:Oracle 11.2.0.4.0和使用JPA 2.1 / Hibernate的PostgreSql 9.2.9

解决方案

我知道它来不及了,但是我们遇到了类似的问题Oracle 11g,JPA2.0,此提示不起作用。



实际上,问题是我们将其用作@NamedQuery提示并正在调用该函数在@transactional方面。由于@NamedQuery在上下文加载时被加载和编译,因此该超时被事务超时覆盖
您可以在 http://javadeveloperz0ne.blogspot.in/2015/07/why-jpa-hints-on-namedquery-wont-work.html



解决方案将再次获取命名查询,然后应用超时。

 查询查询= entityManager.createNamedQuery( NamedQueryName); 
query.setHint( org.hibernate.timeout, 5);
query.getSingleResult();

希望有帮助!


I would like to set a timeout on a javax.persistence.TypedQuery.

I've found this easy method :

TypedQuery<Foo> query = ... ;
query.setHint("javax.persistence.query.timeout", 1000);
query.getReturnList();

But it seems that does not work, it's just ignored.

From the PRO JPA 2 book:

Does anyone knows any other method to specify a timeout on a TypedQuery?

Or how can I make this "hint" working?

Thanks

EDIT: Oracle 11.2.0.4.0 and PostgreSql 9.2.9with JPA 2.1 / Hibernate

解决方案

I know its late to reply, but we faced similar problem Oracle 11g, JPA2.0 and this hint wasn't working.

Actually the problem was we were using it as @NamedQuery hint and were calling the function inside @transactional aspect. As @NamedQuery gets loaded and compiled at the time of context load this timeout was overridden by transaction timeout.You can find more info at http://javadeveloperz0ne.blogspot.in/2015/07/why-jpa-hints-on-namedquery-wont-work.html.

Solution would be fetching named query again and then applying timeout.

Query query = entityManager.createNamedQuery("NamedQueryName");
query.setHint("org.hibernate.timeout", "5");
query.getSingleResult();

Hope it helps!

这篇关于使用JPA2在TypedQuery上设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 08:38