使用JPA2在TypedQuery上设置超时 [英] Set timeout on a TypedQuery with JPA2

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

问题描述

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



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

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

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

PRO JPA 2 书中:


不幸的是,设置查询超时并不是可移植的行为,并非所有数据库平台都支持它
,也不是所有持久性提供者都必须支持的要求
,因此,想要的应用程序
要启用查询超时,必须为三种
情况做好准备。



第一个是该属性被忽略,并且没有任何作用。



第二个是启用该属性,并且运行超过指定超时值的所有select,update或
删除操作都会中止
,并抛出QueryTimeoutException

第三种情况是正确的。该异常可能是由
处理的,不会导致任何活动事务被标记为
回滚。 ty已启用,但这样做
时,如果超时超过
,数据库将强制进行事务回滚。在这种情况下,将抛出PersistenceException并将
事务标记为回滚。通常,如果启用了
应用程序,则应将其编写为处理QueryTimeoutException,但如果超过了超时且未引发
异常,则
应该不会失败。


有人知道其他方法可以在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:

"Unfortunately, setting a query timeout is not portable behavior. It may not be supported by all database platforms nor is it a requirement to be supported by all persistence providers. Therefore, applications that want to enable query timeouts must be prepared for three scenarios.

The first is that the property is silently ignored and has no effect.

The second is that the property is enabled and any select, update, or delete operation that runs longer than the specified timeout value is aborted, and a QueryTimeoutException is thrown. This exception may be handled and will not cause any active transaction to be marked for rollback.

The third scenario is that the property is enabled, but in doing so the database forces a transaction rollback when the timeout is exceeded. In this case, a PersistenceException will be thrown and the transaction marked for rollback. In general, if enabled the application should be written to handle the QueryTimeoutException, but should not fail if the timeout is exceeded and the exception is not thrown."

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.9 with 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上设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆