0

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_logs table every time there is an insert or update on the orders table.

Problem:

When a setter() method is called on an OrderEntity instance, the database logs indicate that the trigger is run for every two setter() method calls within the same transaction. This results in the order_logs table 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.

FourtyTwo
  • 734
  • 8
  • 19
  • Did you try the constraint trigger? Constraints are not just for foreign keys, and this type of trigger seems like it can be used for any logic. – Chris Jul 12 '22 at 13:24

1 Answers1

0

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.

FourtyTwo
  • 734
  • 8
  • 19
  • There still will be a trigger executed for every update statement. What was triggering your flushes if changing the flush-mode to commit is the answer? – Chris Jul 19 '22 at 13:04
  • You are correct. There will still be a trigger but only one at the end of the transaction. This is what I was looking for. Remember the problem was that updates/flushes to the database were made for every 2 or more calls of any setter method on the same entity. – FourtyTwo Jul 20 '22 at 14:10