Hello everyone
I am using Hibernate 4.1.6 as JPA provider. I am trying to use the javax.persistence.query.timeout setting in persistence.xml, but it turns out it doesn't work.
For example, I add this into persistence.xml to set the query timeout as 5 seconds:
Code:
<property name="javax.persistence.query.timeout" value="5000"/>
I use SQL Server as database. Begin a transaction, and update one row, not commit it. So the row has been locked. Then I use JPA to load the entity or run some queries, and these operations should be blocked because of the exclusive lock. The query timeout is supposed to work in this case. However the program was waiting like forever.
It will be fine if I use Query Hint, but it's not acceptable because it's needed for every places and it seems doesn't work for EntityManager.find().
I've also tested other JPA provider such as EclipseLink. The query timeout setting works.
Is this a bug? This setting is stated in documentation:
http://docs.jboss.org/hibernate/core/4.0/hem/en-US/html/configuration.html#setup-configuration-packaging
Here's my testing program:
MyUser.javaCode:
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class MyUser {
@NotNull
@Size(min = 2)
private String firstName;
@NotNull
@Size(min = 2)
private String lastName;
private int age;
}
MyUserIntegrationTestCode:
@Transactional(propagation = Propagation.REQUIRES_NEW)
private void runQuery(MyUser myUser) {
// 1. test get one
em.find(MyUser.class, myUser.getId());
// 2. test query
Query query = em.createQuery("select o from MyUser o where o.age = " + myUser.getAge());
// query.setHint("javax.persistence.query.timeout", 5000);
try {
query.getResultList();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
// 3. test typed query
TypedQuery<MyUser> typedQuery = em.createQuery(
"select o from MyUser o where o.age = " + myUser.getAge(), MyUser.class);
// typedQuery.setHint("javax.persistence.query.timeout", 5000);
try {
typedQuery.getResultList();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
It works for the #2 case, but doesn't work for the #1 and #3 cases.