Skip to content

Commit

Permalink
feat: flush clear 기능 annotation 기반 AOP로 전환
Browse files Browse the repository at this point in the history
  • Loading branch information
kihwankim committed Apr 22, 2022
1 parent a5a3fc1 commit 2123d83
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
@@ -0,0 +1,14 @@
package com.yapp.sharefood.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ModifyingQuery {
boolean flushAuto() default false;

boolean cleanAuto() default false;
}
@@ -0,0 +1,36 @@
package com.yapp.sharefood.aop;

import com.yapp.sharefood.annotation.ModifyingQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;

@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class ModifingQueryAspect {
private final EntityManager entityManager;

@Around("@annotation(modifyingQuery)")
public Object flushAndClearAroundQuery(ProceedingJoinPoint proceedingJoinPoint, ModifyingQuery modifyingQuery) throws Throwable {
if (modifyingQuery.flushAuto()) {
entityManager.flush();
log.debug("before query flush");
}

Object response = proceedingJoinPoint.proceed();

if (modifyingQuery.cleanAuto()) {
entityManager.clear();
log.debug("before query clear");
}

return response;
}
}
Expand Up @@ -5,6 +5,7 @@
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.yapp.sharefood.annotation.ModifyingQuery;
import com.yapp.sharefood.category.domain.Category;
import com.yapp.sharefood.common.exception.BadRequestException;
import com.yapp.sharefood.common.order.SortType;
Expand All @@ -24,7 +25,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -45,7 +45,6 @@ public class FoodQueryRepositoryImpl implements FoodQueryRepository {
private static final Long EMPTY_LIKE_NUMBER = 0L;

private final JPAQueryFactory queryFactory;
private final EntityManager entityManager;

@Override
public List<Food> findFoodWithCategoryByIds(List<Long> ids) {
Expand Down Expand Up @@ -254,15 +253,14 @@ public List<Food> findMineBookMarkFoodSearch(User ownerUser, FoodMinePageSearch
.fetch();
}

@ModifyingQuery(flushAuto = true, cleanAuto = true)
public void updateFoodNumberOfLikesForAtomic(Food updateFood, int numberOfChange) {
if (Objects.isNull(updateFood.getId())) throw new UserNotFoundException();

entityManager.flush();
queryFactory.update(food)
.set(food.numberOfLikes, food.numberOfLikes.add(numberOfChange))
.where(food.id.eq(updateFood.getId()))
.execute();
entityManager.clear();
}

private BooleanExpression containMainAddTag() {
Expand Down

0 comments on commit 2123d83

Please sign in to comment.