mercredi 4 mars 2015

Spring jpa hibernate: read and write data in atomic way



I'm using spring 4 with jpa and hibernate and I'm facing the following issue:


I've a table with the following fields: - ID (PK) - Provider - ProviderId - Status


this table is used to map a workflow on orders coming from a given provider. Provider and ProviderId should be unique but I could have different rows for the same (PRovider, ProviderId) in error statuses while I must have in any moment a maximum of one row for a given Provider,ProviderId in status OPEN. For example, the following is a right configuration:



1,PROV,001,CANCELLED
2,PROV,001,ERROR
3,PROV,001,OPEN


While this one is not valid:



1,PROV1,002,CANCELLED
2,PROV1,002,OPEN
3,PROV1,002,OPEN


I've a service class with an addORder method that checks if for the given Provider,OrderID already exists a row in status OPEN before performing the save. How can I be sure that the check (performing a select) and the db write are performed atomically?? Should the @Transactional annotation work for me? I could add a trigger. Is any smoother way? thanks a lot!




Aucun commentaire:

Enregistrer un commentaire