카테고리 없음

spring java config transaction

머룽 2023. 4. 19. 09:55

spring java config transaction

저번 포스팅에서 rollback이 안되는 문제 있었다. rollback도 안됐지만 service 계층의 영속성도 안됐다(물론 당연한 얘기지만) 해결은 했는데 잘 모르겠다. RootContext를 버리니 잘 된다. 그래서 다시 책을 봤다. 하지만 안보인다.(대충 봐서) 다시 생각이 나서 김영한님의 github를 봤다. RootContext가 없다. 솔직히 아직 잘 모르겠다. root와 servlet context의 대해.. 토비님 책을 다시 읽어야 겠다. servlet이 하나라면 굳이 분리할 필요도 없다고 하는거 같기도 하고.. 아무튼 지금은 잘모르겠다. 일단 나중에 다시 살펴보겠다. 이렇게 바꾸었다.
@Configuration
@EnableWebMvc
@ComponentScan(
  basePackages = "me.wonwoo.account"
)
@Import(RootConfiguration.class)
public class MvcConfiguration extends WebMvcConfigurerAdapter {
  //...
}
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
//    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
//    rootContext.register(RootConfiguration.class);

    AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
    dispatcherServlet.register(MvcConfiguration.class);
    servletContext.addListener(new ContextLoaderListener(dispatcherServlet));

   //...
   //...
}
롤백도 되고 service에 영속성도 된다. 소스가 다시 개판 되었다..ㅡㅡ save하면서 한건입력 후 강제로 에러를 냈다. 아무튼 소스는 다시 올려놔야 겠다.
@Transactional
public Account save(Account account) {
  accountRepository.save(account);
  throw new RuntimeException();
}
에러는 내는 부분
@Transactional(readOnly = true)
public Account findOne(Long id) {
  Account one = accountRepository.findOne(id);
  Product order = one.getOrder();
  order.getOrderedName();
  return one;
}
@OneToOne(fetch = FetchType.LAZY)
service 계층에 영속성 테스트 하는 부분 물론 일단 지금은 EAGER로 바꿔놨다.