Issue
We are migrating our PersistenceUnit from RESOURCE_LOCAL
to JTA
. In the remove cases like below, the Exception it's not raised without an em.flush() after the em.remove().
public boolean delete(T t) {
try {
em.remove(em.contains(t) ? t : em.merge(t));
return true;
} catch (Exception e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
return false;
}
}
Debugging the code in NetBeans, the system enter the return true
and THEN do the database transaction.
This behavior it's not wrong? Using em.flush()
solve the problem but it's the right approach? Cause in RESOURCE_LOCAL
we didn't use and works just fine.
Solution
The EntityManager delays the database operations because it might be able to process them more efficiently. For instance if you deleted two entities it would be able to remove them with a single DELETE statement.
If your application needs the delete to happen immediately, calling flush()
is probably fine.
Answered By - Guillaume
Answer Checked By - Senaida (JavaFixing Volunteer)