[Bug #954] Fix bug

Allow to do flush before doing validations, so the optimist locking
exceptions don't happen at validation phase. When validating, the
exceptions are wraped, so the optimist locking failure can't be easily
seen.

FEA: ItEr74S04BugFixing
This commit is contained in:
Óscar González Fernández 2011-05-26 19:55:43 +02:00
parent 10da8d9e2b
commit 9e4b329791
3 changed files with 29 additions and 12 deletions

View file

@ -84,14 +84,21 @@ public class GenericDAOHibernate<E extends BaseEntity,
return entityClass;
}
public void save(E entity) throws ValidationException {
save(entity, Mode.AUTOMATIC_FLUSH);
}
/**
* It's necessary to save and validate later.
*
* Validate may retrieve the entity from DB and put it into the Session, which can eventually lead to
* a NonUniqueObject exception. Save works here to reattach the object as well as saving.
*/
public void save(E entity) throws ValidationException {
public void save(E entity, Mode mode) throws ValidationException {
getSession().saveOrUpdate(entity);
if (mode == Mode.FLUSH_BEFORE_VALIDATION) {
getSession().flush();
}
entity.validate();
}

View file

@ -42,7 +42,19 @@ public interface IGenericDAO <E, PK extends Serializable>{
public Class<E> getEntityClass();
public enum Mode {
FLUSH_BEFORE_VALIDATION, AUTOMATIC_FLUSH;
}
/**
* It saves with automatic flush
*
* @see #save(Object, Mode)
*/
public void save(E entity) throws ValidationException;
/**
* <p>
* It inserts the object passed as a parameter in the ORM session, planning
* it for updating (even though it is not modified before or after the call
* to this method) or insertion, depending if it is was detached or
@ -51,10 +63,15 @@ public interface IGenericDAO <E, PK extends Serializable>{
* executed (if the entity has version control enabled) with the possible
* <code>org.springframework.dao.OptimisticLockingFailureException</code>
* being thrown.
* </p>
* <p>
*
* </p>
*
* @throws ValidationException
* if the entity has some invalid values
*/
public void save(E entity) throws ValidationException;
public void save(E entity, Mode mode) throws ValidationException;
/**
* Unlike <code>save</code>, it does not execute validations.

View file

@ -25,7 +25,6 @@ import static org.navalplanner.web.I18nHelper._;
import java.util.ArrayList;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -36,6 +35,7 @@ import org.navalplanner.business.calendars.daos.IBaseCalendarDAO;
import org.navalplanner.business.calendars.entities.BaseCalendar;
import org.navalplanner.business.common.daos.IConfigurationDAO;
import org.navalplanner.business.common.daos.IEntitySequenceDAO;
import org.navalplanner.business.common.daos.IGenericDAO.Mode;
import org.navalplanner.business.common.entities.Configuration;
import org.navalplanner.business.common.entities.EntityNameEnum;
import org.navalplanner.business.common.entities.EntitySequence;
@ -48,7 +48,6 @@ import org.navalplanner.web.common.concurrentdetection.OnConcurrentModification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -140,15 +139,9 @@ public class ConfigurationModel implements IConfigurationModel {
@Override
@Transactional
public void confirm() {
checkEntitySequences();
try {
configurationDAO.save(configuration);
storeAndRemoveEntitySequences();
} catch (HibernateOptimisticLockingFailureException e) {
throw new ConcurrentModificationException(
_("Some entity sequence was created during the configuration process, it is impossible to update entity sequence table. Please, try again later"));
}
configurationDAO.save(configuration, Mode.FLUSH_BEFORE_VALIDATION);
storeAndRemoveEntitySequences();
}
private void checkEntitySequences() {