(This is basically the following on this question .)
I need to access the database service level in one of my form validators (to make sure that the email message is not yet received when registering a new user).
I tried the following (some input fields are omitted for brevity):
public class RegistrationPage extends WebPage { @Inject private UserService userService; public RegistrationPage() { add(new FeedbackPanel("feedback")); TextField<String> email = new TextField<String>("email", Model.of("")); ... email.add(new IValidator<String>() { @Override public void validate(IValidatable<String> validatable) { String email = validatable.getValue(); if (userService.findUserByEmail(email) != null) {
Unfortunately, this can lead to
java.lang.IllegalStateException: EntityManager is closed
I suspect that the problem is that I am using open-session-in-view and that several form requests span multiple requests. userService is introduced for the first request and (illegally) reused in subsequent requests. (Multiple forms submission occurs if validation fails and the user tries to submit the form again.)
My question
What is the best way to solve this problem? In the same way as I solved the previous, similar problem ? In this case, it will undoubtedly become useless.
source share