Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stop JPA entity manager from flushing before every select
    text
    copied!<p>I am using JPA (with hibernate as the provider) in spring for a web-app. I have a few methods that construct a complex object to be passed to the UI. As part of theses methods flow, a few <code>select</code> SQL statements are executed. The problem is that the entity manager flushes before each select, this action takes a lot of time and is hindering the performance. Is there a way to prevent the entity manager from flushing before each select? (I don't care for stale data in the select selects above)</p> <p>Thank you.</p> <p>Here is my GenericDAO</p> <pre><code>@Repository public abstract class GenericDAOWithJPA&lt;T, ID extends Serializable&gt; implements IGenericDAO&lt;T, ID&gt; { private static final int MAX_RETRIES = 3; private static final long WAIT_INTERVAL_MS = 1000; static final Logger LOG = LoggerFactory.getLogger(GenericDAOWithJPA.class); private Class&lt;T&gt; persistentClass; protected EntityManager entityManager; @SuppressWarnings("unchecked") public GenericDAOWithJPA() { this.persistentClass = (Class&lt;T&gt;) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; } @Override @PersistenceContext public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; this.entityManager.setFlushMode(FlushModeType.COMMIT) } @Override public Class&lt;T&gt; getPersistentClass() { return persistentClass; } /* (non-Javadoc) * @see com.legolas.dao.IGenericDAO#find(ID) */ @Override public T find(ID id) { return entityManager.find(persistentClass, id); } @Override public T getReference(ID id) { return entityManager.getReference(persistentClass, id); } /* (non-Javadoc) * @see com.legolas.dao.IGenericDAO#persist(T) */ @Override public void persist(T entity) { entityManager.persist(entity); } @Override public void persist(List&lt;T&gt; entityList) { for (T entity : entityList) { persist(entity); } } @Override public T merge(T entity) { return entityManager.merge(entity); } @Override public void remove(T entity) { entityManager.remove(entity); } @Override @SuppressWarnings("unchecked") public List&lt;T&gt; findAll() { return entityManager.createQuery("Select t from " + persistentClass.getSimpleName() + " t").getResultList(); } @Override @SuppressWarnings("unchecked") public List&lt;T&gt; findInRange(int firstResult, int maxResults) { return entityManager.createQuery("Select t from " + persistentClass.getSimpleName() + " t").setFirstResult(firstResult).setMaxResults(maxResults).getResultList(); } @Override public long count() { return (Long) entityManager.createQuery("Select count(t) from " + persistentClass.getSimpleName() + " t").getSingleResult(); } @Override public void flush() { this.entityManager.flush(); } @Override public void refresh(T entity) { int retry = 0; RuntimeException lastException = null; do { try { this.entityManager.refresh(entity); } catch (OptimisticLockException e) { retry++; lastException = e; LOG.debug("OptimisticLockException retry {}", retry); try { Thread.sleep(WAIT_INTERVAL_MS); } catch (InterruptedException e1) { retry = MAX_RETRIES; } } } while (lastException != null &amp;&amp; retry &lt; MAX_RETRIES); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload