Entitiy Manager does not respond before user request ends

I use Hibernate and Oracle SQL in my project. When I call the createNativeQuery method of the entity manager, the entity manager does not answer any call (even from different browsers) until the method returns. The request takes a lot of time, but it is called in a new thread. Why is the object manager blocked?

NOTE. When I call the JPQL query, the problem disappears.

@PersistenceContext
private EntityManager entityManager;

//blocking other transactions, cannot make any read from the same entityManager until this method is completed.
@Transactional(readOnly=true)
public void testMethod1(String query) {

    Query q = entityManager.createNativeQuery(query);
// CANNOT SET LOCKMODE because it is not JPQL :  q.setLockMode(LockModeType.NONE)  //throws exception
    List<Object[]> result =  q.getResultList();
}

@Transactional(readOnly=true)
public void testMethod2(String jpql) {

    Query q = entityManager.createQuery(jpql);
    List<Object> result =  q.getResultList();
}
+4
source share
1 answer

I was able to reproduce your problem using other databases (SQL Anywhere in my case).

Hibernate JavaDocs, , - , JPA setLockMode .

, Hibernate QueryHints :

NATIVE_LOCKMODE: SQL-, JPA , Query.setLockMode(javax.persistence.LockModeType) IllegalStateException, .

QueryHints, - :

@Transactional(readOnly=true)
public void testMethod1(String query) {

    Query q = entityManager.createNativeQuery(query);
    q.setHint(QueryHints.NATIVE_LOCKMODE, LockModeType.NONE);
    List<Object[]> result =  q.getResultList();
}

, @TransactionAttribute , , :

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void testMethod1(String query) {

    Query q = entityManager.createNativeQuery(query);
    q.setHint(QueryHints.NATIVE_LOCKMODE, LockModeType.NONE);
    List<Object[]> result =  q.getResultList();
}

, , , !

0

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


All Articles