How to stop the JPA object manager from flushing before each choice

I am using JPA (with hibernation as a provider) in spring for a web application. I have several methods that build a complex object for passing to the user interface. As part of the flow of these abstracts, several SQL select statements are executed. The problem is that the object manager is reset before each choice, this action takes a lot of time and hinders performance. Is there a way to prevent the object manager from being cleared before each selection? (I do not need outdated data in the choices above)

Thanks.

Here is my GenericDAO

 @Repository public abstract class GenericDAOWithJPA<T, ID extends Serializable> implements IGenericDAO<T, ID> { 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<T> persistentClass; protected EntityManager entityManager; @SuppressWarnings("unchecked") public GenericDAOWithJPA() { this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; } @Override @PersistenceContext public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; this.entityManager.setFlushMode(FlushModeType.COMMIT) } @Override public Class<T> 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<T> 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<T> findAll() { return entityManager.createQuery("Select t from " + persistentClass.getSimpleName() + " t").getResultList(); } @Override @SuppressWarnings("unchecked") public List<T> 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 && retry < MAX_RETRIES); } } 
+4
source share
3 answers

The following code disables the flash before requests (the flash will occur only before committing):

 em.setFlushMode(FlushModeType.COMMIT); 
+5
source

You can also set the flash mode for each request. It should not apply to the entire administrator of an entity.

  TypedQuery<Person> people = ... ; people.setFlushMode(FlushModeType.COMMIT); 
+4
source

If you really run your own SELECT , what about querying the connection provider for a new connection? Thus, Hibernate will be out of order for these requests.

0
source

Source: https://habr.com/ru/post/1341819/


All Articles