Tom
Tom

Reputation: 116

Question on time taken by Controller to call Service - Spring Boot

I have a very basic Controller (Rest) -> Service -> Repository call as given below in Spring Boot. I was debugging the time taken by each step, it looks like there a significant percentage of time is taken by Controller to call the Service method. In the below example -

Total time: 60ms

Time taken by Controller to call Service method (marked: //POINT B): 30ms
Time taken by Service to call Repository method (marked: //POINT C): 1ms
Time taken by Repository to return data from DB: 20ms ......

My question/doubt is on the 30ms taken by the first step. There is no application logic involved between POINT A and B. Is the 30ms because of any initialization that Spring has to do each time Controller tries to call the Service? 30ms is not long, but since it was taking a significant amount of time of the overall processing, I was wondering if I was doing any Spring configuration wrong.

Controller

@Autowired
ContactService contactService;

@RequestMapping(value = "/contact/{contactId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ContactResponse> fetchContactById(@PathVariable Integer contactId) throws Exception {
  ContactResponse contact = new ContactResponse();

  //POINT A
  contact.setContact(contactService.findContactById(contactId));
  .......
  return new ResponseEntity<ContactResponse>(contact, HttpStatus.OK);
}


Service class

@Autowired
ContactRepository contactRepository;

@Service
public class ContactService {

  //POINT B
  public ContactDTO findContactById(Integer contactId) throws Exception {
    return contactRepository.findContactById(contactId);
  }

}

Repository

@Repository
public class ContactRepository {

  //POINT C
  public ContactDTO findContactById(Integer contactId) {
    .......
    return contactDTO;
  }

}

Upvotes: 0

Views: 297

Answers (1)

Andrey B. Panfilov
Andrey B. Panfilov

Reputation: 6063

First of all, I would recommend to perform diagnostics using profiler (IntelliJ has build-in one, jprofiler is better but it costs, it has trial period though).

However, based on your laconic diagnostics, I would say that it is related to autocommit settings: by default autocommit is enabled, so, when transaction starts Hibernate tries to disable autocommit via acquiring connection from connection pool and issuing java.sql.Connection.setAutoCommit(false), which takes time.

Upvotes: 1

Related Questions