EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0

    jdbcTemplate으로 로그인을 구현 도중 아래와 EmptyResultDataAsccessException 에러(Error)가 발생하였는데 이는 QueryForObject 일 경우 값이 없을 때 발생하는 에러이다.

     

    에러발생

    org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
    	at org.springframework.dao.support.DataAccessUtils.nullableSingleResult(DataAccessUtils.java:97) ~[spring-tx-5.3.18.jar:5.3.18]
    	at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:887) ~[spring-jdbc-5.3.18.jar:5.3.18]
    	at com.example.webstart.dao.MemberJdbcDAO.loginChk(MemberJdbcDAO.java:31) ~[classes/:na]
    	at com.example.webstart.dao.MemberJdbcDAO$$FastClassBySpringCGLIB$$ed8de352.invoke(<generated>) ~[classes/:na]
    	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.18.jar:5.3.18]
    	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:783) ~[spring-aop-5.3.18.jar:5.3.18]
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.18.jar:5.3.18]
    	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753) ~[spring-aop-5.3.18.jar:5.3.18]
    	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-5.3.18.jar:5.3.18]
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.18.jar:5.3.18]
    	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:753) ~[spring-aop-5.3.18.jar:5.3.18]
    	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:698) ~[spring-aop-5.3.18.jar:5.3.18]
    	at com.example.webstart.dao.MemberJdbcDAO$$EnhancerBySpringCGLIB$$c8c5073e.loginChk(<generated>) ~[classes/:na]

     

    이 문제를 해결하기 위해서는 Try ~ Catch로 QueryForObject를 예외처리 해야 한다.

     

    As-Is 코드

    public MemberVO loginChk(Map<String, String> paramMap) {
        MemberVO member = jdbcTemplate.queryForObject(
            "select member_id, email, name from member where email = ? AND password = ?",
            (resultSet, rowNum) -> {
                MemberVO newMember = new MemberVO();
                newMember.setEmail(resultSet.getString("email"));
                newMember.setMember_id(resultSet.getString("member_id"));
                newMember.setName(resultSet.getString("name"));
                return newMember;
            }, paramMap.get("email"), paramMap.get("password"));
        return member;
    }

    위 코드 내용에서 EmptyResultDataAccessException를 Try ~ Catch 문으로 만들어서 예외처리를 한다.

     

     

    To-Be 코드

    public MemberVO loginChk(Map<String, String> paramMap) {
        try {
            MemberVO member = jdbcTemplate.queryForObject(
                "select member_id, email, name from member where email = ? AND password = ?",
                (resultSet, rowNum) -> {
                    MemberVO newMember = new MemberVO();
                    newMember.setEmail(resultSet.getString("email"));
                    newMember.setMember_id(resultSet.getString("member_id"));
                    newMember.setName(resultSet.getString("name"));
                    return newMember;
                }, paramMap.get("email"), paramMap.get("password"));
            return member;
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    EmptyResultDataAccessException일 경우 그냥 null값을 리턴하게 처리하였다.

     

    에러가 떨어지지 않고 잘 처리된 모습

     

     

    참고자료의 내용

     

    References

    [1] https://javabydeveloper.com/fix-emptyresultdataaccessexception-incorrect-result-size-expected-1-actual-0/

    반응형

    댓글

    Designed by JB FACTORY