-
Notifications
You must be signed in to change notification settings - Fork 0
[Refactor] ConstraintViolationException 파싱 로직 유틸로 통합 #176
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/main/java/com/semosan/api/common/exception/ConstraintViolationUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.semosan.api.common.exception; | ||
|
|
||
| import org.hibernate.exception.ConstraintViolationException; | ||
|
|
||
| import java.util.regex.Pattern; | ||
|
|
||
| public final class ConstraintViolationUtils { | ||
|
|
||
| private ConstraintViolationUtils() {} | ||
|
|
||
| public static boolean isViolation(Throwable exception, String constraintName) { | ||
| Pattern constraintNamePattern = Pattern.compile( | ||
| "(?<![A-Za-z0-9_])" + Pattern.quote(constraintName) + "(?![A-Za-z0-9_])" | ||
| ); | ||
| Throwable current = exception; | ||
| while (current != null) { | ||
| if (current instanceof ConstraintViolationException constraintViolation | ||
| && constraintName.equals(constraintViolation.getConstraintName())) { | ||
| return true; | ||
| } | ||
| String message = current.getMessage(); | ||
| // 드라이버마다 다른 예외 래핑에 대응하되, 다른 제약명 일부와 겹치는 오탐은 막는다. | ||
| if (message != null && constraintNamePattern.matcher(message).find()) { | ||
| return true; | ||
| } | ||
| current = current.getCause(); | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/test/java/com/semosan/api/common/exception/ConstraintViolationUtilsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package com.semosan.api.common.exception; | ||
|
|
||
| import org.hibernate.exception.ConstraintViolationException; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.dao.DataIntegrityViolationException; | ||
|
|
||
| import java.sql.SQLException; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class ConstraintViolationUtilsTest { | ||
|
|
||
| private static final String CONSTRAINT_NAME = "uk_user_blocks_blocker_blocked"; | ||
|
|
||
| @Test | ||
| void isViolationReturnsTrueWhenHibernateConstraintNameMatches() { | ||
| ConstraintViolationException cause = new ConstraintViolationException( | ||
| "duplicate", | ||
| new SQLException("unique violation"), | ||
| CONSTRAINT_NAME | ||
| ); | ||
|
|
||
| boolean result = ConstraintViolationUtils.isViolation( | ||
| new DataIntegrityViolationException("duplicate", cause), | ||
| CONSTRAINT_NAME | ||
| ); | ||
|
|
||
| assertThat(result).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void isViolationReturnsTrueWhenMessageContainsExactConstraintToken() { | ||
| DataIntegrityViolationException exception = new DataIntegrityViolationException( | ||
| "ERROR: duplicate key value violates unique constraint \"" + CONSTRAINT_NAME + "\"" | ||
| ); | ||
|
|
||
| boolean result = ConstraintViolationUtils.isViolation(exception, CONSTRAINT_NAME); | ||
|
|
||
| assertThat(result).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void isViolationReturnsFalseWhenMessageContainsOnlyConstraintNamePrefix() { | ||
| DataIntegrityViolationException exception = new DataIntegrityViolationException( | ||
| "ERROR: duplicate key value violates unique constraint \"" + CONSTRAINT_NAME + "_idx\"" | ||
| ); | ||
|
|
||
| boolean result = ConstraintViolationUtils.isViolation(exception, CONSTRAINT_NAME); | ||
|
|
||
| assertThat(result).isFalse(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P3] 정규식 패턴 매번 컴파일하는 성능 비효율 개선
isViolation메서드가 호출될 때마다Pattern.compile을 사용하여 정규식 패턴을 매번 새로 생성하고 있습니다. 예외 처리 흐름이라 하더라도, 동시 요청이 많거나 예외가 빈번히 발생하는 상황에서는 CPU 자원을 불필요하게 소모할 수 있습니다.제약 조건 이름(
constraintName)별로 컴파일된Pattern객체를 캐싱하여 재사용하도록 개선하는 것을 권장합니다.ConcurrentHashMap을 사용해 간단히 캐싱할 수 있습니다.References