I have a Java application that connects to a Postgres DB using EclipseLink.
My problem is that database triggers are triggered before the Java/EclipseLink transaction is completed causing the data to be incorrect.
Example:
There is a trigger to update a
order_logstable every time there is aninsertorupdateon theorderstable.
Problem:
When a
setter()method is called on anOrderEntityinstance, the database logs indicate that the trigger is run for every twosetter()method calls within the same transaction. This results in theorder_logstable having multiple rows for only one update.
My question is that is there a way to force/ensure that the trigger is triggered only at the end of a transaction. I had a look at this page but deferring looks like it’s just for foreign keys. My methods are wrapped with the @Transactional annotation.
The trigger was created using:
create trigger my_insert_update_trigger after
insert or update
on
public.my_table for each row execute procedure my_insert_update_trigger_function();
Advertisement
Answer
I ended up having to add this line:
<property name="eclipselink.persistence-context.flush-mode" value="COMMIT" />
to the persistence.xml file as documented in persistence-context.flush-mode and this answer.