Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backend] Close entity manager #1883

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,27 @@ public Page<AssetGroupOutput> assetGroupPagination(
}

public List<AssetGroupOutput> find(Specification<AssetGroup> specification) {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<AssetGroup> root = cq.from(AssetGroup.class);
select(cb, cq, root);
try {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<AssetGroup> root = cq.from(AssetGroup.class);
select(cb, cq, root);

if (specification != null) {
Predicate predicate = specification.toPredicate(root, cq, cb);
if (predicate != null) {
cq.where(predicate);
}
}

if (specification != null) {
Predicate predicate = specification.toPredicate(root, cq, cb);
if (predicate != null) {
cq.where(predicate);
TypedQuery<Tuple> query = entityManager.createQuery(cq);
return execution(query, this.mapper);
} finally {
if (entityManager != null && entityManager.isOpen()) {
entityManager.close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Thank you! I close it! :)

}
}

TypedQuery<Tuple> query = entityManager.createQuery(cq);
return execution(query, this.mapper);
}

// -- PRIVATE --
Expand All @@ -62,37 +68,43 @@ private Page<AssetGroupOutput> paginate(
Specification<AssetGroup> specification,
Specification<AssetGroup> specificationCount,
Pageable pageable) {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<AssetGroup> assetGroupRoot = cq.from(AssetGroup.class);
select(cb, cq, assetGroupRoot);

// -- Specification --
if (specification != null) {
Predicate predicate = specification.toPredicate(assetGroupRoot, cq, cb);
if (predicate != null) {
cq.where(predicate);
try {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<AssetGroup> assetGroupRoot = cq.from(AssetGroup.class);
select(cb, cq, assetGroupRoot);

// -- Specification --
if (specification != null) {
Predicate predicate = specification.toPredicate(assetGroupRoot, cq, cb);
if (predicate != null) {
cq.where(predicate);
}
}
}

// -- Sorting --
List<Order> orders = toSortCriteriaBuilder(cb, assetGroupRoot, pageable.getSort());
cq.orderBy(orders);
// -- Sorting --
List<Order> orders = toSortCriteriaBuilder(cb, assetGroupRoot, pageable.getSort());
cq.orderBy(orders);

// Type Query
TypedQuery<Tuple> query = entityManager.createQuery(cq);
// Type Query
TypedQuery<Tuple> query = entityManager.createQuery(cq);

// -- Pagination --
query.setFirstResult((int) pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
// -- Pagination --
query.setFirstResult((int) pageable.getOffset());
query.setMaxResults(pageable.getPageSize());

// -- EXECUTION --
List<AssetGroupOutput> assetGroups = execution(query, this.mapper);
// -- EXECUTION --
List<AssetGroupOutput> assetGroups = execution(query, this.mapper);

// -- Count Query --
Long total = countQuery(cb, this.entityManager, AssetGroup.class, specificationCount);
// -- Count Query --
Long total = countQuery(cb, this.entityManager, AssetGroup.class, specificationCount);

return new PageImpl<>(assetGroups, pageable, total);
return new PageImpl<>(assetGroups, pageable, total);
} finally {
if (entityManager != null && entityManager.isOpen()) {
entityManager.close();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,40 +338,46 @@ public Page<ExerciseSimple> exercises(
Specification<Exercise> specificationCount,
Pageable pageable,
Map<String, Join<Base, Base>> joinMap) {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<Exercise> exerciseRoot = cq.from(Exercise.class);
select(cb, cq, exerciseRoot, joinMap);

// -- Text Search and Filters --
if (specification != null) {
Predicate predicate = specification.toPredicate(exerciseRoot, cq, cb);
if (predicate != null) {
cq.where(predicate);
try {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<Exercise> exerciseRoot = cq.from(Exercise.class);
select(cb, cq, exerciseRoot, joinMap);

// -- Text Search and Filters --
if (specification != null) {
Predicate predicate = specification.toPredicate(exerciseRoot, cq, cb);
if (predicate != null) {
cq.where(predicate);
}
}
}

// -- Sorting --
List<Order> orders = toSortCriteriaBuilder(cb, exerciseRoot, pageable.getSort());
cq.orderBy(orders);
// -- Sorting --
List<Order> orders = toSortCriteriaBuilder(cb, exerciseRoot, pageable.getSort());
cq.orderBy(orders);

// Type Query
TypedQuery<Tuple> query = entityManager.createQuery(cq);
// Type Query
TypedQuery<Tuple> query = entityManager.createQuery(cq);

// -- Pagination --
query.setFirstResult((int) pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
// -- Pagination --
query.setFirstResult((int) pageable.getOffset());
query.setMaxResults(pageable.getPageSize());

// -- EXECUTION --
List<ExerciseSimple> exercises = execution(query);
// -- EXECUTION --
List<ExerciseSimple> exercises = execution(query);

setComputedAttribute(exercises);
setComputedAttribute(exercises);

// -- Count Query --
Long total = countQuery(cb, this.entityManager, Exercise.class, specificationCount);
// -- Count Query --
Long total = countQuery(cb, this.entityManager, Exercise.class, specificationCount);

return new PageImpl<>(exercises, pageable, total);
return new PageImpl<>(exercises, pageable, total);
} finally {
if (entityManager != null && entityManager.isOpen()) {
entityManager.close();
}
}
}

// -- SELECT --
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,38 +67,44 @@ public Page<InjectorContractOutput> injectorContracts(
@Nullable final Specification<InjectorContract> specification,
@Nullable final Specification<InjectorContract> specificationCount,
@NotNull final Pageable pageable) {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<InjectorContract> injectorContractRoot = cq.from(InjectorContract.class);
selectForInjectorContract(cb, cq, injectorContractRoot);

// -- Text Search and Filters --
if (specification != null) {
Predicate predicate = specification.toPredicate(injectorContractRoot, cq, cb);
if (predicate != null) {
cq.where(predicate);
try {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<InjectorContract> injectorContractRoot = cq.from(InjectorContract.class);
selectForInjectorContract(cb, cq, injectorContractRoot);

// -- Text Search and Filters --
if (specification != null) {
Predicate predicate = specification.toPredicate(injectorContractRoot, cq, cb);
if (predicate != null) {
cq.where(predicate);
}
}
}

// -- Sorting --
List<Order> orders = toSortCriteriaBuilder(cb, injectorContractRoot, pageable.getSort());
cq.orderBy(orders);
// -- Sorting --
List<Order> orders = toSortCriteriaBuilder(cb, injectorContractRoot, pageable.getSort());
cq.orderBy(orders);

// Type Query
TypedQuery<Tuple> query = this.entityManager.createQuery(cq);
// Type Query
TypedQuery<Tuple> query = this.entityManager.createQuery(cq);

// -- Pagination --
query.setFirstResult((int) pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
// -- Pagination --
query.setFirstResult((int) pageable.getOffset());
query.setMaxResults(pageable.getPageSize());

// -- EXECUTION --
List<InjectorContractOutput> injectorContractOutputs = execInjectorContract(query);
// -- EXECUTION --
List<InjectorContractOutput> injectorContractOutputs = execInjectorContract(query);

// -- Count Query --
Long total = countQuery(cb, this.entityManager, InjectorContract.class, specificationCount);
// -- Count Query --
Long total = countQuery(cb, this.entityManager, InjectorContract.class, specificationCount);

return new PageImpl<>(injectorContractOutputs, pageable, total);
return new PageImpl<>(injectorContractOutputs, pageable, total);
} finally {
if (entityManager != null && entityManager.isOpen()) {
entityManager.close();
}
}
}

// -- CRITERIA BUILDER --
Expand Down
56 changes: 31 additions & 25 deletions openbas-api/src/main/java/io/openbas/rest/user/PlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,43 @@ private Page<PlayerOutput> paginate(
Specification<User> specification,
Specification<User> specificationCount,
Pageable pageable) {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<User> userRoot = cq.from(User.class);
select(cb, cq, userRoot);

// -- Specification --
if (specification != null) {
Predicate predicate = specification.toPredicate(userRoot, cq, cb);
if (predicate != null) {
cq.where(predicate);
try {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<User> userRoot = cq.from(User.class);
select(cb, cq, userRoot);

// -- Specification --
if (specification != null) {
Predicate predicate = specification.toPredicate(userRoot, cq, cb);
if (predicate != null) {
cq.where(predicate);
}
}
}

// -- Sorting --
List<Order> orders = toSortCriteriaBuilder(cb, userRoot, pageable.getSort());
cq.orderBy(orders);
// -- Sorting --
List<Order> orders = toSortCriteriaBuilder(cb, userRoot, pageable.getSort());
cq.orderBy(orders);

// Type Query
TypedQuery<Tuple> query = entityManager.createQuery(cq);
// Type Query
TypedQuery<Tuple> query = entityManager.createQuery(cq);

// -- Pagination --
query.setFirstResult((int) pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
// -- Pagination --
query.setFirstResult((int) pageable.getOffset());
query.setMaxResults(pageable.getPageSize());

// -- EXECUTION --
List<PlayerOutput> players = execution(query);
// -- EXECUTION --
List<PlayerOutput> players = execution(query);

// -- Count Query --
Long total = countQuery(cb, this.entityManager, User.class, specificationCount);
// -- Count Query --
Long total = countQuery(cb, this.entityManager, User.class, specificationCount);

return new PageImpl<>(players, pageable, total);
return new PageImpl<>(players, pageable, total);
} finally {
if (entityManager != null && entityManager.isOpen()) {
entityManager.close();
}
}
}
}
Loading