Skip to content

Instantly share code, notes, and snippets.

@jpukg
Created May 12, 2022 13:29
Show Gist options
  • Save jpukg/39d05856da764fa38814211bb6e7743b to your computer and use it in GitHub Desktop.
Save jpukg/39d05856da764fa38814211bb6e7743b to your computer and use it in GitHub Desktop.
Getting large number of records from database without having primary cache + Separate entity manager
public List<Employee> findAllEmployee(int offset, int limit) {
String employeeQuery = String.format("select distinct r from %s r left join r.owner as u", Employee.class);
employeeQuery += " WHERE r.status = :status ORDER BY r.lastModification DESC";
EntityManagerFactory factory = Persistence.createEntityManagerFactory("employee_ds");
EntityManager shortLiveEntityManager = factory.createEntityManager();
TypedQuery<RISIndex> query = shortLiveEntityManager.createQuery(employeeQuery, Employee.class).setFlushMode(FlushModeType.COMMIT);;
query.setParameter("status", Status.APPROVED);
query.setHint(QueryHints.QUERY_RESULTS_CACHE, false);
query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheThenDatabase);
List<RISIndex> resultList = query
.setFirstResult(offset) // offset
.setMaxResults(limit + offset) // limit
.getResultList();
shortLiveEntityManager.close();
return resultList;
}
@jpukg
Copy link
Author

jpukg commented May 12, 2022

Annotate with TransactionManagementType.BEAN , so that the container (Weblogic, etc) timeout will not be occurred (default timeout of 30 seconds)

@Stateless
@Slf4j
@TransactionManagement(TransactionManagementType.BEAN)
public class ExportTableSnapshotsServiceImpl implements ExportTableSnapshotsService {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment