Custom validator, issue with entityManager
mechtatel Aug 24, 2009 10:36 PMHi,
I'm implementing custom validator like seam component and it work fine. The issue appear where I add aj4:support to the input field and when I quickly change and back the focus on the mentioned field, multiple ajax request are fired and offten I get:
/Connection handle has been closed and is unusable/
and after that often is raised:
/Closing a connection for you. Please close them yourself/
If I put the decorate code snipet in a region and append it to status I get a debug page.
All the exceptions and the debug page are shown in detail below.
I'm using Jboss tools with Seam 3.0.1.GA-R200905070146-H18 and richfaces 3.3.1
The project is using only POJOs.
This is the custom validator code:
package package.path; import java.io.Serializable; import java.util.List; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.ValidatorException; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import path.entity.User; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Logger; import org.jboss.seam.annotations.Scope; import org.jboss.seam.annotations.Transactional; import org.jboss.seam.annotations.faces.Validator; import org.jboss.seam.log.Log; import org.jboss.seam.faces.FacesMessages; @Name("userNameValidator") @Validator @Scope(ScopeType.CONVERSATION) public class UserNameValidator implements javax.faces.validator.Validator, Serializable { @In EntityManager entityManager; @Transactional public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { entityManager.joinTransaction(); String login = (String) value; Query query = entityManager.createQuery("select u from path.entity.User u where u.login = :login"); query.setParameter("login", login); query.getResultList(); if (query.getResultList().size() > 0) { throw new ValidatorException(makeMessage("The faces message")); } } private FacesMessage makeMessage(String s) { FacesMessage message = new FacesMessage(); message.setDetail(s); message.setSummary(s); message.setSeverity(FacesMessage.SEVERITY_ERROR); return message; } }
This is the view code:
<s:decorate id="usernameDecorator" template="layout/edit.xhtml"> <ui:define name="label">Username:</ui:define> <h:inputText id="rusername" value="#{register.userNameReg}" required="true"> <f:validator validatorId="userNameValidator"/> <a4j:support event="onblur" reRender="usernameDecorator" ignoreDupResponses="true" bypassUpdates="true" ajaxSingle="true" /> </h:inputText> <rich:message for="rusername"> <f:facet name="passedMarker"> <h:graphicImage style="margin: 0 0 4px 4px" value="/img/passed.gif" /> </f:facet> <f:facet name="errorMarker"> <h:graphicImage style="margin: 0 0 4px 4px" value="/img/error.gif" /> </f:facet> </rich:message> </s:decorate>