<aside> 💡

@Transactional self-invocation (in effect, a method within the target object calling another method of the target object) does not lead to an actual transaction at runtime

</aside>

image.png

채팅방 목록 조회 api 개발 중 lastMessage와 lastestTime이 업데이트가 안되는 이슈가 발생했다.

image.png

Spring AOP의 @Transactional 자기호출(self-invocation) 문제

Spring의 @Transactional은 기본적으로 프록시(proxy) 기반 AOP로 동작

즉,

내 기존 업데이트 메서드 : 트랜잭션을 붙였다.

image.png

해당 메서드를 사용하는 곳인 chat메서드에도 아래와 같이 @Transactional 어노테이션이 붙어있다.

image.png

로그를 찍어보니

[Hibernate] 
    select
        c1_0.id,
        c1_0.created_at,
        c1_0.deleted_at,
        c1_0.latest_content,
        c1_0.latest_content_time,
        c1_0.product_id,
        c1_0.receiver_id,
        c1_0.room_name,
        c1_0.sender_id,
        c1_0.updated_at 
    from
        chatroom c1_0 
    where
        c1_0.id=?
[Hibernate] 
    insert 
    into
        message
        (chatroom_id, content, created_at, deleted_at, receiver_id, sender_id, updated_at) 
    values
        (?, ?, ?, ?, ?, ?, ?)