I need to delete a list of employees ignoring any integrity constraints (i.e. successfully delete the entity that doesn't related to any others yet or skip the entity that related to others) using JPA 2.0 and EJB3.0 on glassfish3.0:
I iterated on the list and call entity manager in a require_new transaction within a try/catch, I expect this will open small-nested transactions within the big transaction and I use em.flush() to force the updates.
but within iteration of a related entity it throws;
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.2.v20130514-5956486): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails
and continue to the next employee but at the commit of the big transaction it rollbacks and throw the same exception!
I expect to throw/catch the exception with in the new-small transaction and commit the successfully deleted entities of the rest of the list...
do I miss anything?
EDIT:
Here is some code, the code in the Facade to call the entity manager to remove the bean, I annotate the function by required-new:
public void deleteEmployeeData(Set<Employees> deleted) throws DatabaseException {
Iterator itr;
Employees emp;
//delete unused recordes
try {
itr = deleted.iterator();
while (itr.hasNext()) {
emp = (Employees) itr.next();
deleteEmployee(emp);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("DONE");
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
private void deleteEmployee(Employees emp){
try {
this.remove(emp);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
deleteEmployee
method is not wrapped into a new transaction because you are referencing method onthis
. What you can do is to inject reference to the facade itself and then calldeleteEmployee
method on it (it should be public).More or less something like this: