so this question has been asked a few times already but seems like no one answered it in a way that could help me. I'm currently making a backend for a simple application handling product data. It's not even with JSP, just a plain Rest backend. Using RestControllers by Spring.
The "problem" is: the first request after startup takes way longer to get an answer from the server than all others. (I'm just testing with Postman with a simple JPA User Entity)
Some things to consider:
- it's probably not a database problem in itself since it clearly just initializes something upon first incoming request instead of on startup
- In the log, it says "Initializing Spring DispatcherServlet 'dispatcherServlet'" when the first actual request comes in (via Postman).
- If I pull all users from the database (which currently is only one user), the first request after startup takes 140ms (according to Postman). After that, the same request needs always 10ms at max.
- There's a flag some answer to a similar question suggested: spring.mvc.servlet.load-on-startup=1. Though this only removes the logging (of the initialization of the DispatcherServlet) mentioned above.
- It seems like this is standard behavior which has nothing to do with how I actually coded my entities and/or RestControllers.
How do I make the first request faster / how do I force Spring to actually initialize everything before the first request comes in?
Some code anyways:
User.java:
@Entity
@Table(name = "users")
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NonNull
private String firstName;
@NonNull
private String lastName;
@NonNull
@OneToOne(cascade = CascadeType.ALL)
private Address billingAddress;
//a bit more. a list and another address
}
UserController.java:
@RestController
@RequestMapping("users")
public class UserController {
private final UserRepository userRepository;
@Autowired
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping()
public List<User> getAllUsers() {
return (List<User>) userRepository.findAll();
}
//more stuff
}