-
1. Re: Handling database exceptions
gavin.king Jun 26, 2007 8:30 PM (in response to shakenbrain)You can't. If a flush fails and an exception propagates out of the database, there is no practical way for the ORM to figure out what the current state of the database is, and resynchronize the in-memory state to that.
Check your constraint first. -
2. Re: Handling database exceptions
shakenbrain Jun 27, 2007 12:01 PM (in response to shakenbrain)Understood. Gavin, thanks for your response.
Getting back to validating then, it appears that using a @Validator class causes the session to go invalid when it throws a ValidatorException, but only on the whole form submit (it works if you do a partial form submit using ajax4jsf on just that one field, using <a:support>). Here's a validator class that does nothing but throw the exception:@Validator @Name("prefixValidator") public class PrefixValidator extends BaseValidator { @In EntityManager entityManager; // @Transactional public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { throw new ValidatorException(makeMessage("Prefix is being used already.")); } }
and the relevent xhtml:<s:decorate id="prefixDecorator" template="/decorateField.xhtml"> <ui:define name="label">Prefix</ui:define> <h:inputText id="prefix" size="4" value="#{selectedOrganization.prefix}" required="true" disabled="#{!organizationAdmin.new}"> <f:validator validatorId="prefixValidator"/> </h:inputText> </s:decorate>
That should work right? Is this a bug? I haven't tried this in 2.0 Beta yet, I'll try to do that today. -
3. Re: Handling database exceptions
gavin.king Jun 27, 2007 12:34 PM (in response to shakenbrain)It will only happen if (a) the EM gets enlisted in a tx and (b) the tx gets rolled back.
-
4. Re: Handling database exceptions
shakenbrain Jun 27, 2007 12:44 PM (in response to shakenbrain)ValidatorException being thrown shouldn't cause the transaction to roll back, right?
-
5. Re: Handling database exceptions
gavin.king Jun 27, 2007 12:45 PM (in response to shakenbrain)It did until this latest release (2.0beta)