해결 : @Mapper 작성으로 오류 해결
pic:org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@5fc02f29
p: SignUpPostReq(uid=mic123, upw=12121, nm=홍길동1, userId=0, pic=null)
@Transactional // 프로필 이미지 처리 public int postUser(MultipartFile pic, SignUpPostReq p) { // pic.getOriginalFilename(); // 업로드 된 파일의 이름을 가져올 수 있는 메서드 (문자열을 반환) String saveFileName = customFileUtils.makeRandomFileName(pic); p.setPic(saveFileName); int result = mapper.postUser(p); try { String path = String.format("user/%d", p.getUserId()); // 폴더를 만들때 필요한 정보 customFileUtils.makeFolders(path); String target = String.format("%s/%s", path, saveFileName); // 폴더 + 파일명 customFileUtils.transferTo(pic, target); // 이동 } catch (Exception e) { e.printStackTrace(); } return mapper.postUser(p); // ----> return result; 로 수정 }
@Transactional 어노테이션을 사용하여 메소드를 트랜잭션으로 처리하고 있음.
하지만 코드를 보면 mapper.postUser(p);가 메소드 내에서 두 번 호출되고 있음을 확인할 수 있음
첫 번째 호출에서 이미 데이터베이스에 사용자 정보를 성공적으로 삽입한 후, 같은 사용자 정보로 또 다시 삽입을 시도하기 때문에 java.sql.SQLIntegrityConstraintViolationException: (conn=54) Duplicate entry 'mic1' for key 'uid' 와 같은 오류가 발생
DB에서 uid가 유니크 키로 설정되어 있기 때문에 같은 uid 값을 가진 레코드를 두 번 삽입하려고 할 때 제약 조건 위반 예외가 발생
[2024-05-14 10:33:46 ERROR http-nio-8080-exec-7] [org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.jdbc.UncategorizedSQLException:
Error updating database. Cause: java.sql.SQLException: unknown escape sequence {userId}
The error may exist in file [D:\2024-1\Backend\Practice\build\resources\main\mappers\UserMapper.xml]
The error may involve com.green.practice.user.UserMapper.patchPassword The error occurred while executing an update
SQL: UPDATE user SET upw = ? where user_id = {userId}
Cause: java.sql.SQLException: unknown escape sequence {userId} ; uncategorized SQLException; SQL state [null]; error code [0]; unknown escape sequence {userId}] with root cause java.sql.SQLException: unknown escape sequence {userId}
- 해결 : SQL 쿼리에서 발생한 에러.
{userId}는 SQL에서 인식할 수 없는 이스케이프 시퀀스로 인식되었음.
이 문제를 해결하기 위해서는 SQL 쿼리 내의 변수 바인딩 방식을 확인<update id="patchPassword"> UPDATE user SET upw = #{newPw} where user_id = {userId} <!-- #{userId}로 수정 --> </update>
- 해결
UserMapper만 사용하고 있었고 위에 xml 3개는 미사용(완전 공백)으로 발생한 에러.
xml 3개 삭제로 해결
- 해결
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.green.greengram.user.UserMapper">
- xml 파일과 UserMapper 인터페이스 주소값이 틀려서 발생한 에러
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.green.grampractice.user.UserMapper">
- 수정으로 오류해결
프로젝트 중 파일 업로드를 했는데 이와 같은 예외가 발생하였다.
.gif 용량이 12MB라서 15도 해보고 30MB까지도 올려봤으나 계속 똑같은 에러가 발생하였다.
max-file-size specifies the maximum size permitted for uploaded files. The default is 1MB
max-request-size specifies the maximum size allowed for multipart/form-data requests. The default is 10MB.
링크를 살펴보면
따라서 max-file-size를 초과하지 않아도 서버측으로 전송되는 모든 리소스의 크기의 합이 max-request-size를 초과한다면 요청은 차단된다.
하지만 max-file-size를 초과한다면 max-request-size를 초과하든 안하든 요청은 차단된다.
- 해결
Spring Boot를 이용하고 있기 때문에 application yaml 파일을 이용하여 다음처럼 간단하게 변경하여 문제를 해결 할 수 있다.
이 경우 각 파일은 30MB를 초과할 수 없고 서버로 전송되는 모든 데이터가 50MB를 초과할 수 없다.