How to set the timeout period on a JPA EntityManager query

Yes, there javax.persistence.query.timeout. According JPA 2.0 specification support for this query hint is optional:

Portable applications should not rely on this hint. Depending on the
persistence provider and database in use, the hint may or may not be
observed.

Default value (in milliseconds) can be set to persistence.xml for all queries:

<property name="javax.persistence.query.timeout" value="1000"/>

Same property can be also given when creating EntityManagerFactory via Persistence.createEntityManagerFactory.

It can also be overridden/set per query:

query.setHint("javax.persistence.query.timeout", 2000);

Same functionality can be obtained via attribute hints in NamedQuery.

Leave a Comment