<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>

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

Spring AOP의 @Transactional 자기호출(self-invocation) 문제
Spring의 @Transactional은 기본적으로 프록시(proxy) 기반 AOP로 동작
즉,
this.다른Transactional메서드()처럼 호출하면, 프록시를 거치지 않고 바로 자기 자신(원본 인스턴스)을 호출해버림@Transactional이 무시되어 트랜잭션이 시작되지 않음내 기존 업데이트 메서드 : 트랜잭션을 붙였다.

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

로그를 찍어보니
[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
(?, ?, ?, ?, ?, ?, ?)