Closed white3km closed 3 weeks ago
Elide 7 adds support in the Spring autoconfiguration to use the Spring Platform Transaction Manager by default. This means that your Repository operations are taking place in the same transaction as what Elide is using. This means that the persistent entity being passed into the hook is actually the exact same persistent entity instance as that retrieved using findById
since it's essentially sharing the same EntityManager. ie. elideEntity == original
Prior to Elide 7, the Elide operations and the Repository operations would be performed in separate transactions since they are not aware of each other. So in that case your Repository operation will be pulling from the database as it's creating a separate transaction and the entity isn't already present in the EntityManager associated with that transaction.
To achieve your previous behavior of creating a new transaction to retrieve the old records you can inject a @Service
with @Transactional(propagation = Propagation.REQUIRES_NEW)
into your AlphaHook
so that Spring will start a new transaction when you call findById
. Your hook will then use the AlphaService
instead of directly using the AlphaRepository
.
@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class AlphaService {
private final AlphaRepository alphaRepository;
public AlphaService (AlphaRepository alphaRepository) {
this.alphaRepository = alphaRepository;
}
public Optional<Alpha> findById(UUID id) {
return alphaRepository.findById(id);
}
}
Interesting, that's very helpful information. I see the default in the config now (https://github.com/yahoo/elide/blob/master/elide-spring/elide-spring-boot-autoconfigure/src/main/java/com/yahoo/elide/spring/orm/jpa/PlatformJpaTransaction.java).
Thanks for the info and the quick response!
Previously my project used a hook for updates to pull the original entity from the database and perform some business logic with the database version of the entity. With the update to Elide 7 I've noticed the database version of the entity already has updated data despite being the Pre-Security or Pre-Flush phase.
Expected Behavior
Config
Entity
Repository
Hook
Patch Request (/api/alpha/0cc11d88-a853-4cf0-9848-e14490b51313)
Expected Flow
Expected Log
Current Behavior
Seems like the update is being saved to the transaction earlier than PRE hooks.
Actual Log
If I move the hooks to the textValue field
I still get the same result for database value
If I change the operation to Delete
Then the phases seem to be performing properly (entity is not found after flush)
Context
While I realize you can annotate individual fields and utilize the ChangeSpec for individual field changes, we found that we needed the full entity to run our business logic on update operations. That's why we prefer the class level hook and with repository approach.
Your Environment