Example usage for org.springframework.orm.jpa EntityManagerHolder hasTimeout

List of usage examples for org.springframework.orm.jpa EntityManagerHolder hasTimeout

Introduction

In this page you can find the example usage for org.springframework.orm.jpa EntityManagerHolder hasTimeout.

Prototype

public boolean hasTimeout() 

Source Link

Document

Return whether this object has an associated timeout.

Usage

From source file:org.springframework.orm.jpa.EntityManagerFactoryUtils.java

/**
 * Apply the current transaction timeout, if any, to the given JPA Query object.
 * <p>This method sets the JPA 2.0 query hint "javax.persistence.query.timeout" accordingly.
 * @param query the JPA Query object/*from   w  ww .  jav  a 2 s.c om*/
 * @param emf JPA EntityManagerFactory that the Query was created for
 */
public static void applyTransactionTimeout(Query query, EntityManagerFactory emf) {
    EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(emf);
    if (emHolder != null && emHolder.hasTimeout()) {
        int timeoutValue = (int) emHolder.getTimeToLiveInMillis();
        try {
            query.setHint("javax.persistence.query.timeout", timeoutValue);
        } catch (IllegalArgumentException ex) {
            // oh well, at least we tried...
        }
    }
}