코딍코딍
코딩기록
코딍코딍
전체 방문자
오늘
어제
  • 분류 전체보기 (271)
    • 개발 (2)
    • Java (1)
    • 스프링 (28)
    • JPA (11)
    • Git (3)
    • 알고리즘 (160)
      • 백준 (132)
      • 프로그래머스 (8)
      • SWEA (20)
    • 토이 프로젝트 (14)
      • 간단한 Springboot CRUD (1)
      • 게시판 프로젝트 (13)
    • 알고리즘 개념정리 (8)
    • 오류 해결 (13)
    • 보류 (0)
    • AWS (5)
    • 트러블 슈팅 (0)
    • 회고 (3)
    • CS (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

최근 글

티스토리

hELLO · Designed By 정상우.
코딍코딍

코딩기록

오류 해결

[Spring Data JPA] No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call 오류 해결

2023. 4. 19. 14:41

원인

@SpringBootApplication
@EnableJpaAuditing
public class AloharoomBackendApplication implements CommandLineRunner {
	public static void main(String[] args) {
		SpringApplication.run(AloharoomBackendApplication.class, args);
	}

	@PersistenceContext
	EntityManager em;
	
	@Override
	public void run(String... args) throws Exception {
		//Home 생성
		BoardAddDto b1 = new BoardAddDto();
		b1.setFlat(30); b1.setRoomCount(3); b1.setRent(25); b1.setHomeType("apartment"); b1.setStartDate(LocalDate.now());
		b1.setAddress("서울특별시 성북구 삼선교로16길 116"); b1.setContents("방 게시물1 내용입니다.");
		Home h1 = new Home(b1);
		
		em.persist(h1);
		...

실행 시 초기 데이터를 삽입하기 위해 위와 같이 코드를 작성했으나

현재 스레드에 EntityManager가 없어서 persist 할 수 없다고 한다.

 

해결

"actual transaction available for current thread" 라고 쓰여있어서 내 작업에 트랜잭션이 선언되어 있나 확인해 보았는데 역시 선언이 안 되어있었다.

기본적으로 JPA는 Transaction 기반으로 작동하게 되어있다.

Transaction 단위에 따라 1차캐시영역에 있는 객체들이 db에 flush 되어 영속화되기 때문이다. 하지만 그러한 영속작업을 하는 persist() 메소드에 객체가 들어갔으나 가능한 transaction이 존재하지 않았기에 저런 에러를 낸 것이다.

 

참고

https://sas-study.tistory.com/348

 

'오류 해결' 카테고리의 다른 글

[Spring] ddl-auto: create 안 됨  (1) 2023.10.24
[JPA] More than one row with the given identifier was found: 1  (0) 2023.03.11
[Mysql] Cannot delete or update a parent row  (0) 2023.02.28
Java Mysql 연동 시 오류 발생 : java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed  (0) 2023.02.13
java.lang.StackOverflowError: null - @Data  (0) 2023.01.11
    '오류 해결' 카테고리의 다른 글
    • [Spring] ddl-auto: create 안 됨
    • [JPA] More than one row with the given identifier was found: 1
    • [Mysql] Cannot delete or update a parent row
    • Java Mysql 연동 시 오류 발생 : java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
    코딍코딍
    코딍코딍
    ㅎ2

    티스토리툴바