Reputation: 1032
My application runs using Persistence API, Hibernate and Spring. I am using MySql DB. There are some queries which might take a lot of time to compute, so I need a cancel button to cancel the execution of these queries.
I found out that there is a memthod java.sql.statement.cancel() which is used to cancel a query, but this can be accesssed from the statement object, how to do this from the entityManager object in Java persistence ?
Upvotes: 0
Views: 2390
Reputation: 1809
You can use hibernate session objects CancelQuery() method.
Also if you want to automate this, you can set the query timeout. JPA 2 defines the javax.persistence.query.timeout hint to specify default timeout in milliseconds. (Available since Hibernate 3.5 )
JPA provides javax.persistence.query.timeout hint to specify query timeout in milliseconds. This hint can be specified in @NamedQuery or @NamedNativeQuery. It can also be specified on the Query or TypedQuery by calling the setHint method. The query timeout defined by the hint can be overridden by the setHint method.
The default query timeout can be specified for an application in persistence.xml as shown below:
<persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
...
<property name="javax.persistence.query.timeout"
value="30" />
</properties>
</persistence-unit>
Upvotes: 2