게시글
질문&답변
특정 repository를 이용해 save 후 DB에도 반영된 정보가 해당 repository 를 통해 find 시 DB반영된 정보를 가지고 오지 못하는 현상에 대한 질문드립니다.
답변 감사합니다. 스스로 더 파악해보고, 문제 해결되면 공유하겠습니다.
- 0
- 6
- 385
질문&답변
특정 repository를 이용해 save 후 DB에도 반영된 정보가 해당 repository 를 통해 find 시 DB반영된 정보를 가지고 오지 못하는 현상에 대한 질문드립니다.
간단한 샘플을 만들어서 재현이 되는 것을 확인했고, 아래 링크에 재현 되는 소스를 공유했습니다. https://gitlab.com/xoska74/jpademo 앞서 말씀드린것 처럼 controller파트에서 아래와 같이(thread를 생성하지 않고) 호출하면 @RequestMapping(value = "/test", method = RequestMethod.GET) public Callable test() { return () -> { testService.test(); return true; }; } 출력되는 로그는 아래와 같고,(comment를 completed로 업데이트해서 find시에 completed가 나타날 것을 예상했으나, 변경되지 않고 comment에 no comment가 나타남) 2020-09-12 11:52:55,443 DEBUG [task-3 ] o.hibernate.SQL : /* insert com.tnkim.jpademo.model.test.TestModel */ insert into test_model (comment, name, id) values (?, ?, ?) Hibernate: /* insert com.tnkim.jpademo.model.test.TestModel */ insert into test_model (comment, name, id) values (?, ?, ?) 2020-09-12 11:52:55,447 TRACE [task-3 ] o.h.t.d.s.BasicBinder : binding parameter [1] as [VARCHAR] - [no comment] 2020-09-12 11:52:55,448 TRACE [task-3 ] o.h.t.d.s.BasicBinder : binding parameter [2] as [VARCHAR] - [mac] 2020-09-12 11:52:55,449 TRACE [task-3 ] o.h.t.d.s.BasicBinder : binding parameter [3] as [BIGINT] - [8] 2020-09-12 11:52:55,461 INFO [task-3 ] c.t.j.s.TestService : saved model: TestModel(id=8, name=mac, comment=no comment) 2020-09-12 11:52:55,482 INFO [task-3 ] c.t.j.s.TestService : found TestModel: TestModel(id=8, name=mac, comment=no comment) 2020-09-12 11:52:55,482 DEBUG [Thread-20 ] o.hibernate.SQL : select testmodel0_.id as id1_0_0_, testmodel0_.comment as comment2_0_0_, testmodel0_.name as name3_0_0_ from test_model testmodel0_ where testmodel0_.id=? Hibernate: select testmodel0_.id as id1_0_0_, testmodel0_.comment as comment2_0_0_, testmodel0_.name as name3_0_0_ from test_model testmodel0_ where testmodel0_.id=? 2020-09-12 11:52:55,483 TRACE [Thread-20 ] o.h.t.d.s.BasicBinder : binding parameter [1] as [BIGINT] - [8] 2020-09-12 11:52:55,493 TRACE [Thread-20 ] o.h.t.d.s.BasicExtractor : extracted value ([comment2_0_0_] : [VARCHAR]) - [no comment] 2020-09-12 11:52:55,494 TRACE [Thread-20 ] o.h.t.d.s.BasicExtractor : extracted value ([name3_0_0_] : [VARCHAR]) - [mac] 2020-09-12 11:52:55,508 DEBUG [Thread-20 ] o.hibernate.SQL : /* load com.tnkim.jpademo.model.test.TestModel */ select testmodel0_.id as id1_0_0_, testmodel0_.comment as comment2_0_0_, testmodel0_.name as name3_0_0_ from test_model testmodel0_ where testmodel0_.id=? Hibernate: /* load com.tnkim.jpademo.model.test.TestModel */ select testmodel0_.id as id1_0_0_, testmodel0_.comment as comment2_0_0_, testmodel0_.name as name3_0_0_ from test_model testmodel0_ where testmodel0_.id=? 2020-09-12 11:52:55,509 TRACE [Thread-20 ] o.h.t.d.s.BasicBinder : binding parameter [1] as [BIGINT] - [8] 2020-09-12 11:52:55,511 TRACE [Thread-20 ] o.h.t.d.s.BasicExtractor : extracted value ([comment2_0_0_] : [VARCHAR]) - [no comment] 2020-09-12 11:52:55,512 TRACE [Thread-20 ] o.h.t.d.s.BasicExtractor : extracted value ([name3_0_0_] : [VARCHAR]) - [mac] 2020-09-12 11:52:55,514 DEBUG [Thread-20 ] o.hibernate.SQL : /* update com.tnkim.jpademo.model.test.TestModel */ update test_model set comment=?, name=? where id=? Hibernate: /* update com.tnkim.jpademo.model.test.TestModel */ update test_model set comment=?, name=? where id=? 2020-09-12 11:52:55,514 TRACE [Thread-20 ] o.h.t.d.s.BasicBinder : binding parameter [1] as [VARCHAR] - [completed] 2020-09-12 11:52:55,515 TRACE [Thread-20 ] o.h.t.d.s.BasicBinder : binding parameter [2] as [VARCHAR] - [mac] 2020-09-12 11:52:55,515 TRACE [Thread-20 ] o.h.t.d.s.BasicBinder : binding parameter [3] as [BIGINT] - [8] 2020-09-12 11:52:56,491 INFO [task-3 ] c.t.j.s.TestService : found TestModel: TestModel(id=8, name=mac, comment=no comment) 2020-09-12 11:52:57,495 INFO [task-3 ] c.t.j.s.TestService : found TestModel: TestModel(id=8, name=mac, comment=no comment) 2020-09-12 11:52:58,498 INFO [task-3 ] c.t.j.s.TestService : found TestModel: TestModel(id=8, name=mac, comment=no comment) 2020-09-12 11:52:59,505 INFO [task-3 ] c.t.j.s.TestService : found TestModel: TestModel(id=8, name=mac, comment=no comment) 2020-09-12 11:53:00,510 INFO [task-3 ] c.t.j.s.TestService : found TestModel: TestModel(id=8, name=mac, comment=no comment) 2020-09-12 11:53:01,514 INFO [task-3 ] c.t.j.s.TestService : found TestModel: TestModel(id=8, name=mac, comment=no comment) controller파트에서 thread를 생성하여 service의 test()를 호출하면 @RequestMapping(value = "/test", method = RequestMethod.GET) public Callable test() { return () -> { new Thread(() -> { try { testService.test(); } catch (InterruptedException e) { e.printStackTrace(); } }).start(); return true; }; } 출력되는 로그는 아래와 같이 정상적으로 처리되어 출력됩니다.(comment를 completed로 업데이트해서 find시에 completed가 정상적으로 나타남) 2020-09-12 11:56:18,744 DEBUG [Thread-19 ] o.hibernate.SQL : /* insert com.tnkim.jpademo.model.test.TestModel */ insert into test_model (comment, name, id) values (?, ?, ?) Hibernate: /* insert com.tnkim.jpademo.model.test.TestModel */ insert into test_model (comment, name, id) values (?, ?, ?) 2020-09-12 11:56:18,750 TRACE [Thread-19 ] o.h.t.d.s.BasicBinder : binding parameter [1] as [VARCHAR] - [no comment] 2020-09-12 11:56:18,750 TRACE [Thread-19 ] o.h.t.d.s.BasicBinder : binding parameter [2] as [VARCHAR] - [mac] 2020-09-12 11:56:18,751 TRACE [Thread-19 ] o.h.t.d.s.BasicBinder : binding parameter [3] as [BIGINT] - [9] 2020-09-12 11:56:18,764 INFO [Thread-19 ] c.t.j.s.TestService : saved model: TestModel(id=9, name=mac, comment=no comment) 2020-09-12 11:56:18,789 DEBUG [Thread-19 ] o.hibernate.SQL : select testmodel0_.id as id1_0_0_, testmodel0_.comment as comment2_0_0_, testmodel0_.name as name3_0_0_ from test_model testmodel0_ where testmodel0_.id=? 2020-09-12 11:56:18,789 DEBUG [Thread-20 ] o.hibernate.SQL : select testmodel0_.id as id1_0_0_, testmodel0_.comment as comment2_0_0_, testmodel0_.name as name3_0_0_ from test_model testmodel0_ where testmodel0_.id=? Hibernate: select testmodel0_.id as id1_0_0_, testmodel0_.comment as comment2_0_0_, testmodel0_.name as name3_0_0_ from test_model testmodel0_ where testmodel0_.id=? Hibernate: select testmodel0_.id as id1_0_0_, testmodel0_.comment as comment2_0_0_, testmodel0_.name as name3_0_0_ from test_model testmodel0_ where testmodel0_.id=? 2020-09-12 11:56:18,791 TRACE [Thread-20 ] o.h.t.d.s.BasicBinder : binding parameter [1] as [BIGINT] - [9] 2020-09-12 11:56:18,791 TRACE [Thread-19 ] o.h.t.d.s.BasicBinder : binding parameter [1] as [BIGINT] - [9] 2020-09-12 11:56:18,804 TRACE [Thread-20 ] o.h.t.d.s.BasicExtractor : extracted value ([comment2_0_0_] : [VARCHAR]) - [no comment] 2020-09-12 11:56:18,804 TRACE [Thread-19 ] o.h.t.d.s.BasicExtractor : extracted value ([comment2_0_0_] : [VARCHAR]) - [no comment] 2020-09-12 11:56:18,804 TRACE [Thread-20 ] o.h.t.d.s.BasicExtractor : extracted value ([name3_0_0_] : [VARCHAR]) - [mac] 2020-09-12 11:56:18,804 TRACE [Thread-19 ] o.h.t.d.s.BasicExtractor : extracted value ([name3_0_0_] : [VARCHAR]) - [mac] 2020-09-12 11:56:18,813 INFO [Thread-19 ] c.t.j.s.TestService : found TestModel: TestModel(id=9, name=mac, comment=no comment) 2020-09-12 11:56:18,821 DEBUG [Thread-20 ] o.hibernate.SQL : /* load com.tnkim.jpademo.model.test.TestModel */ select testmodel0_.id as id1_0_0_, testmodel0_.comment as comment2_0_0_, testmodel0_.name as name3_0_0_ from test_model testmodel0_ where testmodel0_.id=? Hibernate: /* load com.tnkim.jpademo.model.test.TestModel */ select testmodel0_.id as id1_0_0_, testmodel0_.comment as comment2_0_0_, testmodel0_.name as name3_0_0_ from test_model testmodel0_ where testmodel0_.id=? 2020-09-12 11:56:18,822 TRACE [Thread-20 ] o.h.t.d.s.BasicBinder : binding parameter [1] as [BIGINT] - [9] 2020-09-12 11:56:18,827 TRACE [Thread-20 ] o.h.t.d.s.BasicExtractor : extracted value ([comment2_0_0_] : [VARCHAR]) - [no comment] 2020-09-12 11:56:18,828 TRACE [Thread-20 ] o.h.t.d.s.BasicExtractor : extracted value ([name3_0_0_] : [VARCHAR]) - [mac] 2020-09-12 11:56:18,829 DEBUG [Thread-20 ] o.hibernate.SQL : /* update com.tnkim.jpademo.model.test.TestModel */ update test_model set comment=?, name=? where id=? Hibernate: /* update com.tnkim.jpademo.model.test.TestModel */ update test_model set comment=?, name=? where id=? 2020-09-12 11:56:18,830 TRACE [Thread-20 ] o.h.t.d.s.BasicBinder : binding parameter [1] as [VARCHAR] - [completed] 2020-09-12 11:56:18,830 TRACE [Thread-20 ] o.h.t.d.s.BasicBinder : binding parameter [2] as [VARCHAR] - [mac] 2020-09-12 11:56:18,831 TRACE [Thread-20 ] o.h.t.d.s.BasicBinder : binding parameter [3] as [BIGINT] - [9] 2020-09-12 11:56:19,818 DEBUG [Thread-19 ] o.hibernate.SQL : select testmodel0_.id as id1_0_0_, testmodel0_.comment as comment2_0_0_, testmodel0_.name as name3_0_0_ from test_model testmodel0_ where testmodel0_.id=? Hibernate: select testmodel0_.id as id1_0_0_, testmodel0_.comment as comment2_0_0_, testmodel0_.name as name3_0_0_ from test_model testmodel0_ where testmodel0_.id=? 2020-09-12 11:56:19,818 TRACE [Thread-19 ] o.h.t.d.s.BasicBinder : binding parameter [1] as [BIGINT] - [9] 2020-09-12 11:56:19,820 TRACE [Thread-19 ] o.h.t.d.s.BasicExtractor : extracted value ([comment2_0_0_] : [VARCHAR]) - [completed] 2020-09-12 11:56:19,821 TRACE [Thread-19 ] o.h.t.d.s.BasicExtractor : extracted value ([name3_0_0_] : [VARCHAR]) - [mac] 2020-09-12 11:56:19,824 INFO [Thread-19 ] c.t.j.s.TestService : found TestModel: TestModel(id=9, name=mac, comment=completed) 2020-09-12 11:56:19,824 INFO [Thread-19 ] c.t.j.s.TestService : the comment of the testModel : completed
- 0
- 6
- 385
질문&답변
특정 repository를 이용해 save 후 DB에도 반영된 정보가 해당 repository 를 통해 find 시 DB반영된 정보를 가지고 오지 못하는 현상에 대한 질문드립니다.
선생님, 답변감사합니다~ ^ㅡ^ 현재 아래와 같은 configuration을 설정한 상태에서 HTTP API를 통해 말씀드렸던 처리를 하려고 하는 상황입니다. @Configuration @EnableAsync public class AsyncMVCConfig implements WebMvcConfigurer { @Bean @RequestScope public UserData requestScopeUserData() { return new UserData(); } @Bean public AuthInterceptor interceptor() { return new AuthInterceptor(); } @Bean public AsyncTaskExecutor asyncTaskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(100); executor.setMaxPoolSize(1000); executor.setQueueCapacity(1000); executor.setThreadNamePrefix("task-"); executor.initialize(); return executor; } @Override public void configureAsyncSupport(AsyncSupportConfigurer configurer) { configurer.setTaskExecutor(asyncTaskExecutor()); } } 기존의 서비스를 호출하는 코드는 아래와 같이 AsynTask를 통해 동작하는 controller의 test()가 testService.test()를 호출하는 방식이었고, 말씀드렸던 것처럼 service의 while문 내에서 testModelRepository.findById(id)를 통해 변경된 DB의 data를 가지고 오지 못였습니다.(findById로 가지고 온 최초의 값이 계속 전달되었습니다. 최초 가져온 data를 cache후 계속 변경된 DB내용 이 적용되지 않은채로 cache의 내용을 전달하는 느낌입니다. ) @Autowired TestService testService; @AnonymousCallable @RequestMapping(value = "/test", method = RequestMethod.POST) public Callable test(@ApiParam(required = true, value = "id: 테스트 정보(필수 값)") @RequestBody String jsonString) { return () -> { JSONObject jsonResult = testService.test(JsonUtils.getMap(jsonString)); ResponseObject responseObject = new ResponseObject(); responseObject.putResult(jsonResult); return responseObject; }; } 그런데, 선생님 테스트 하셨던 것 처럼 new Thread()를 통해 testService.test()를 호출하는 방식을 취하니 정상적으로 service의 while문 내에서 testModelRepository.findById(id)를 통해 변경된 DB의 data를 가지고 오네요. @AnonymousCallable @RequestMapping(value = "/test", method = RequestMethod.POST) public Callable test(@ApiParam(required = true, value = "id: 테스트 정보(필수 값)") @RequestBody String jsonString) { return () -> { new Thread(() -> { JSONObject jsonResult = testService.test(JsonUtils.getMap(jsonString)); }).start(); JSONObject jsonResult = new JSONObject(); ResponseObject responseObject = new ResponseObject(); responseObject.putResult(jsonResult); return responseObject; }; } AsyncTask에서 처리하는 것과 Thread에서 처리하는 것의 차이가 있는것 같은데 잘 이해가 되지 않네요. 글로 먼저 써서 올립니다. 재현될 수 있는 예제를 하나 만들어서 다시 질문드리도록 하겠습니다. 감사합니다.
- 0
- 6
- 385