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

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

Introduction

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

Prototype

public long getTimeToLiveInMillis() throws TransactionTimedOutException 

Source Link

Document

Return the time to live for this object in milliseconds.

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/*w w  w .j a  va  2s.c  o m*/
 * @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...
        }
    }
}